找到你要的答案

Q:EF ignoring fluent API for ON DELETE CASCADE

Q:EF忽略了API的级联删除

I have this code:

  modelBuilder.Entity<ProductDetails>()
          .HasRequired(c => c.Product)
          .WithMany()
          .WillCascadeOnDelete(false);

However when I check the table definition generated I see:

 CONSTRAINT [FK_dbo.ProductDetails_dbo.Product_ProductId] FOREIGN KEY ([ProductId]) REFERENCES [dbo].[Product] ([Id]) ON DELETE CASCADE

What am I missing? why does it create the ON DELETE CASCADE?

我有这个代码:

  modelBuilder.Entity<ProductDetails>()
          .HasRequired(c => c.Product)
          .WithMany()
          .WillCascadeOnDelete(false);

但是,当我检查表定义生成时,我看到:

 CONSTRAINT [FK_dbo.ProductDetails_dbo.Product_ProductId] FOREIGN KEY ([ProductId]) REFERENCES [dbo].[Product] ([Id]) ON DELETE CASCADE

我错过了什么?为什么要创建上删除级联?

answer1: 回答1:

I believe there is an issue with EF in the syntax above. When I reverse the definition like described below, it works:

  modelBuilder
         .Entity<Product>()
         .HasMany(p => p.Details)
         .WithRequired()
         .WillCascadeOnDelete(false);

I believe there is an issue with EF in the syntax above. When I reverse the definition like described below, it works:

  modelBuilder
         .Entity<Product>()
         .HasMany(p => p.Details)
         .WithRequired()
         .WillCascadeOnDelete(false);
c#  entity-framework