5

Dlaczego relacje z WillCascadeOnDelete (false) zdefiniowane w konfiguracji są zawsze prawdziwe w wygenerowanej migracji?Usuwanie kaskady Entity Framework Migration jest zawsze prawdziwe, nawet WillCascadeOnDelete (false) w konfiguracji

Oto kod konfiguracji

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

i tu jest wygenerowanym migracji oraz

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

    } 

Odpowiedz

1

To może być głupie pytanie, ale czy nadpisane metody OnModelBuilding w DbContext i dodał modelBuilder.Configurations.Add(new PartySuppliesConfiguration());? Czy PartySuppliers ma jakieś właściwości DataAnnotations, które mogą przesłonić klasę konfiguracji jednostki? A może pochodzi od innej klasy, w której cascadeondelete jest ustawione na true?

+0

Zauważyłem to dzisiaj, gdy nie dodałem metody 'modelBuilder.Configurations.Add (new MyMappingClass()) do metody OnModelBuilding. – DDiVita