EF Core可以使用数据注释配置删除行为。

huangapple go评论60阅读模式
英文:

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: &quot;Staff&quot;,
                columns: table =&gt; new
                {
                    StaffId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
                        .Annotation(&quot;SqlServer:Identity&quot;, &quot;1, 1&quot;),
                    StaffName = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: false),
                    CourseId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false),
                    IsActive = table.Column&lt;bool&gt;(type: &quot;bit&quot;, nullable: false),
                    IsDeleted = table.Column&lt;bool&gt;(type: &quot;bit&quot;, nullable: false)
                },
                constraints: table =&gt;
                {
                    table.PrimaryKey(&quot;PK_Staff&quot;, x =&gt; x.StaffId);
                    table.ForeignKey(
                        name: &quot;FK_Staff_Courses_CourseId&quot;,
                        column: x =&gt; x.CourseId,
                        principalTable: &quot;Courses&quot;,
                        principalColumn: &quot;CourseId&quot;,
                        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&#39;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 =&gt; 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>



huangapple
  • 本文由 发表于 2023年2月24日 00:45:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定