Entity Framework Code First: FOREIGN KEY constraint may cause cycles or multiple cascade paths

前端 未结 2 787
离开以前
离开以前 2020-12-31 04:22

Entity Framework Code First can generate the DB for the following POCOs.

public class Item {
    public int Id { get; set; }
    public string Name { get; se         


        
2条回答
  •  天命终不由人
    2020-12-31 05:10

    I decided to just remove the cascade delete convention.

     protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Conventions.Remove();
            modelBuilder.Conventions.Remove();
    
        }
    

    The reasons are:

    • I prefer to mark records deleted or deactivated for audit purposes.
    • At most I delete just the junction / mapping tables.
    • With an ORM It is relatively trivial to loop through and delete child records in the rare case I need to.

    Thank you Ladislav Mrnka pointing me in the right direction.

提交回复
热议问题