英文:
ef core configure on delete behavior by data annotation
问题
尝试通过数据注解实现外键的删除行为。我尝试了以下方式:
以下是我的模型配置
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
public int CourseType { get; set; }
public bool IsActive { get; set; }
public bool IsDeleted { get; set; }
}
public class Staff
{
public int StaffId { get; set; }
public string StaffName { get; set; }
[Required]
public int CourseId { get; set; }
public virtual Course Course { get; set; }
public bool IsActive { get; set; }
public bool IsDeleted { get; set; }
}
当进行迁移时,生成了以下代码:
migrationBuilder.CreateTable(
name: "Staff",
columns: table => new
{
StaffId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
StaffName = table.Column<string>(type: "nvarchar(max)", nullable: false),
CourseId = table.Column<int>(type: "int", nullable: false),
IsActive = table.Column<bool>(type: "bit", nullable: false),
IsDeleted = table.Column<bool>(type: "bit", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Staff", x => x.StaffId);
table.ForeignKey(
name: "FK_Staff_Courses_CourseId",
column: x => x.CourseId,
principalTable: "Courses",
principalColumn: "CourseId",
onDelete: ReferentialAction.Cascade);
});
现在想要更改删除行为(而不是手动更改)为"Required"而不是"Cascade"。我知道可以使用Fluent API 来实现这一点,但不知道是否可以使用数据注解来实现?
英文:
Trying to achieve foreign key on delete behavior by data annotation.
I tried in the following way
Below are my model configuration
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
public int CourseType { get; set; }
public bool IsActive { get; set; }
public bool IsDeleted { get; set; }
}
public class Staff
{
public int StaffId { get; set; }
public string StaffName { get; set; }
[Required]
public int CourseId { get; set; }
public virtual Course Course { get; set; }
public bool IsActive { get; set; }
public bool IsDeleted { get; set; }
}
When the migration is added it generated below
migrationBuilder.CreateTable(
name: "Staff",
columns: table => new
{
StaffId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
StaffName = table.Column<string>(type: "nvarchar(max)", nullable: false),
CourseId = table.Column<int>(type: "int", nullable: false),
IsActive = table.Column<bool>(type: "bit", nullable: false),
IsDeleted = table.Column<bool>(type: "bit", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Staff", x => x.StaffId);
table.ForeignKey(
name: "FK_Staff_Courses_CourseId",
column: x => x.CourseId,
principalTable: "Courses",
principalColumn: "CourseId",
onDelete: ReferentialAction.Cascade);
});
Now want to change ondelete behavior (not manually) as required instead of cascade.
I know we can achieve it using fluent API but wondering can we achieve it using data annotation ??
答案1
得分: 1
这在EF Core 7中是可能的,使用DeleteBehaviorAttribute。如果您不使用EF Core 7,则可以使用下面更详细描述的代码 这里。否则,您必须使用FluentAPI。
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
<details>
<summary>英文:</summary>
It's possible in EF Core 7 with the [DeleteBehaviorAttribute][1]. If you are not in EF Core 7 then you can use this below code described in more detail [here][2]. Otherwise you have to use FluentAPI.
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
[1]: https://learn.microsoft.com/en-us/ef/core/modeling/relationships/mapping-attributes#deletebehaviorattribute
[2]: https://stackoverflow.com/questions/49326769/entity-framework-core-delete-cascade-and-required
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论