Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Migration cascade delete is always true even WillCascadeOnDelete(false) in configuration

Why relations with WillCascadeOnDelete(false) defined in configuration, are always true in generated migration?

Here is the configuration code

 public class PartySuppliesConfiguration:EntityTypeConfiguration<PartySupplies>
{
    public PartySuppliesConfiguration()
    {
        HasKey(x => x.Id);
        HasRequired(x=>x.Supplier).WithMany().HasForeignKey(x=>x.SupplierId).WillCascadeOnDelete(false);
        HasRequired(x => x.Product).WithMany().HasForeignKey(x => x.ProductId).WillCascadeOnDelete(false);
        HasRequired(x => x.Currency).WithMany().HasForeignKey(x => x.CurrencyId).WillCascadeOnDelete(false);
        HasRequired(x => x.CreatedUser).WithMany().HasForeignKey(x => x.CreatedUserId).WillCascadeOnDelete(false);
        Property(x => x.UnitPrice).HasColumnType("Money");
    }
}

and here is the generated migration

 public override void Up()
    {
        CreateTable(
            "PartySupplies",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    SupplierId = c.Int(nullable: false),
                    ProductId = c.Int(nullable: false),
                    UnitPrice = c.Decimal(nullable: false, precision: 18, scale: 2),
                    CurrencyId = c.Int(nullable: false),
                    FromDate = c.DateTime(nullable: false),
                    ThruDate = c.DateTime(),
                    CreatedUserId = c.Int(nullable: false),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("Parties", t => t.SupplierId, cascadeDelete: true)
            .ForeignKey("Products", t => t.ProductId, cascadeDelete: true)
            .ForeignKey("Curencies", t => t.CurrencyId, cascadeDelete: true)
            .ForeignKey("Users", t => t.CreatedUserId, cascadeDelete: true)
            .Index(t => t.SupplierId)
            .Index(t => t.ProductId)
            .Index(t => t.CurrencyId)
            .Index(t => t.CreatedUserId);

    }
like image 846
accidental coder Avatar asked Nov 12 '22 21:11

accidental coder


1 Answers

It might be a silly question but have you overriden the OnModelBuilding method in DbContext and added modelBuilder.Configurations.Add( new PartySuppliesConfiguration( ) );? Does PartySuppliers have any DataAnnotations that might be overriding your entity config class? Or maybe it's derived from another class where cascadeondelete is set to true?

like image 107
Nathon Fowlie Avatar answered Nov 15 '22 13:11

Nathon Fowlie