2012-09-23 12 views
5

Tại sao quan hệ với WillCascadeOnDelete (false) được định nghĩa trong cấu hình, luôn đúng trong quá trình di chuyển được tạo ra?Entity Framework Migration delete delete luôn luôn đúng ngay cả khi WillCascadeOnDelete (false) trong cấu hình

Đây là mã cấu hình

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

và đây là sự di cư tạo

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

    } 

Trả lời

1

Nó có thể là một câu hỏi ngớ ngẩn nhưng bạn đã overriden phương pháp OnModelBuilding trong DbContext và bổ sung modelBuilder.Configurations.Add(new PartySuppliesConfiguration());? PartySuppliers có bất kỳ Chú thích dữ liệu nào có thể ghi đè lớp cấu hình thực thể của bạn không? Hoặc có thể nó bắt nguồn từ một lớp khác mà cascadeondelete được đặt thành true?

+0

Tôi nhận thấy điều này ngày hôm nay khi tôi không thêm 'modelBuilder.Configurations.Add (new MyMappingClass())' vào phương thức OnModelBuilding. – DDiVita