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

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

ef core configure on delete behavior by data annotation

问题

尝试通过数据注解实现外键的删除行为。我尝试了以下方式:

以下是我的模型配置

  1. public class Course
  2. {
  3. public int CourseId { get; set; }
  4. public string CourseName { get; set; }
  5. public int CourseType { get; set; }
  6. public bool IsActive { get; set; }
  7. public bool IsDeleted { get; set; }
  8. }
  9. public class Staff
  10. {
  11. public int StaffId { get; set; }
  12. public string StaffName { get; set; }
  13. [Required]
  14. public int CourseId { get; set; }
  15. public virtual Course Course { get; set; }
  16. public bool IsActive { get; set; }
  17. public bool IsDeleted { get; set; }
  18. }

当进行迁移时,生成了以下代码:

  1. migrationBuilder.CreateTable(
  2. name: "Staff",
  3. columns: table => new
  4. {
  5. StaffId = table.Column<int>(type: "int", nullable: false)
  6. .Annotation("SqlServer:Identity", "1, 1"),
  7. StaffName = table.Column<string>(type: "nvarchar(max)", nullable: false),
  8. CourseId = table.Column<int>(type: "int", nullable: false),
  9. IsActive = table.Column<bool>(type: "bit", nullable: false),
  10. IsDeleted = table.Column<bool>(type: "bit", nullable: false)
  11. },
  12. constraints: table =>
  13. {
  14. table.PrimaryKey("PK_Staff", x => x.StaffId);
  15. table.ForeignKey(
  16. name: "FK_Staff_Courses_CourseId",
  17. column: x => x.CourseId,
  18. principalTable: "Courses",
  19. principalColumn: "CourseId",
  20. onDelete: ReferentialAction.Cascade);
  21. });

现在想要更改删除行为(而不是手动更改)为"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

  1. public class Course
  2. {
  3. public int CourseId { get; set; }
  4. public string CourseName { get; set; }
  5. public int CourseType { get; set; }
  6. public bool IsActive { get; set; }
  7. public bool IsDeleted { get; set; }
  8. }
  9. public class Staff
  10. {
  11. public int StaffId { get; set; }
  12. public string StaffName { get; set; }
  13. [Required]
  14. public int CourseId { get; set; }
  15. public virtual Course Course { get; set; }
  16. public bool IsActive { get; set; }
  17. public bool IsDeleted { get; set; }
  18. }

When the migration is added it generated below

  1. migrationBuilder.CreateTable(
  2. name: &quot;Staff&quot;,
  3. columns: table =&gt; new
  4. {
  5. StaffId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
  6. .Annotation(&quot;SqlServer:Identity&quot;, &quot;1, 1&quot;),
  7. StaffName = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: false),
  8. CourseId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false),
  9. IsActive = table.Column&lt;bool&gt;(type: &quot;bit&quot;, nullable: false),
  10. IsDeleted = table.Column&lt;bool&gt;(type: &quot;bit&quot;, nullable: false)
  11. },
  12. constraints: table =&gt;
  13. {
  14. table.PrimaryKey(&quot;PK_Staff&quot;, x =&gt; x.StaffId);
  15. table.ForeignKey(
  16. name: &quot;FK_Staff_Courses_CourseId&quot;,
  17. column: x =&gt; x.CourseId,
  18. principalTable: &quot;Courses&quot;,
  19. principalColumn: &quot;CourseId&quot;,
  20. onDelete: ReferentialAction.Cascade);
  21. });

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。

  1. foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
  2. {
  3. relationship.DeleteBehavior = DeleteBehavior.Restrict;
  4. }
  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e =&gt; e.GetForeignKeys()))
  5. {
  6. relationship.DeleteBehavior = DeleteBehavior.Restrict;
  7. }
  8. [1]: https://learn.microsoft.com/en-us/ef/core/modeling/relationships/mapping-attributes#deletebehaviorattribute
  9. [2]: https://stackoverflow.com/questions/49326769/entity-framework-core-delete-cascade-and-required
  10. </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:

确定