实体框架迁移级联删除在配置中甚至是WillCascadeOnDelete(false)也是如此

时间:2012-09-23 14:17:17

标签: entity-framework ef-code-first ef-migrations

为什么在配置中定义与WillCascadeOnDelete(false)的关系在生成的迁移中始终为true?

以下是配置代码

 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");
    }
}

,这是生成的迁移

 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);

    }

1 个答案:

答案 0 :(得分:1)

这可能是一个愚蠢的问题,但你是否重写了DbContext中的OnModelBuilding方法并添加了modelBuilder.Configurations.Add( new PartySuppliesConfiguration( ) );PartySuppliers是否有任何可能覆盖您的实体配置类的DataAnnotations?或者它可能来自cascadeondelete设置为true的另一个类?