2012-09-23 74 views
5

爲什麼與在配置中定義的WillCascadeOnDelete(false)的關係在生成的遷移中始終爲真?實體框架遷移級聯刪除始終爲真即使WillCascadeOnDelete(false)在配置中

下面是配置代碼

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

這可能是一個愚蠢的問題,但你重寫中的DbContext的OnModelBuilding方法和添加modelBuilder.Configurations.Add(new PartySuppliesConfiguration());PartySuppliers是否有可能覆蓋您的實體配置類的DataAnnotations?或者,也許它是從cascadeondelete設置爲true的另一個類派生的?

+0

我今天注意到了這一點,當時我沒有將'modelBuilder.Configurations.Add(new MyMappingClass())'添加到OnModelBuilding方法中。 – DDiVita

相關問題