简体   繁体   English

与 TPT 代码优先实体框架中的引用约束冲突

[英]Conflict with reference constraint in TPT code-first Entity Framework

I am using TPT code-first in Entity Framework 6 and have the following setup:我在 Entity Framework 6 中使用 TPT code-first 并具有以下设置:

public abstract class Product
{
    [Key]
    public string ProductID { get; set; }
    // a bunch of trivial properties like dates and floats
}

[Table("SpecialProducts")]
public class SpecialProduct : Product
{
    // more trivial properties
    public List<Property> MyProperties { get; set; }
}

public class Property
{
    [Key]
    public int ID { get; set; }
    [Required]
    public SpecialProduct Product { get; set; }
    // property data
}

public class MyDbContext : DbContext
{
    public DbSet<Product> AllProducts { get; set; }

    public MyDbContext()
        : base("MyDataBase")
    {}

    public RemoveSomeProducts()
    {
        var products = from product in AllProducts where /* some condition */ select product;
        AllProducts.RemoveRange(products);
        SaveChanges();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // I know I don't need both statements, and my guess is I need the first, but at this point I don't know anything anymore
        modelBuilder.Entity<Property>()
            .HasRequired(property => property.Product)
            .WithMany(product => product.MyProperties)
            .WillCascadeOnDelete(true);

        modelBuilder.Entity<SpecialProduct>()
            .HasMany(product => product.MyProperties)
            .WithRequired(property => property.Product)
            .WillCascadeOnDelete(true);
    }

}

When calling RemoveSomeProducts() I get the following Exception:调用RemoveSomeProducts()出现以下异常:

SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.Properties_dbo.SpecialProducts_Product_ProductID". SqlException: DELETE 语句与 REFERENCE 约束“FK_dbo.Properties_dbo.SpecialProducts_Product_ProductID”冲突。 The conflict occurred in database "MyDataBase", table "dbo.Properties", column 'Product_ProductID'.冲突发生在数据库“MyDataBase”、表“dbo.Properties”、“Product_ProductID”列中。

To me this sounds like the Properties belonging to the deleted SpecialProducts are not being deleted.对我来说,这听起来像是没有删除属于已删除的SpecialProductsProperties I have little experience with databases, but from my understanding this should be fixed using cascade delete, but I seem to fail to configure this.我对数据库的经验很少,但根据我的理解,这应该使用级联删除来修复,但我似乎无法配置它。

So my question is obviously: how can I fix this?所以我的问题显然是:我该如何解决这个问题?

Potential duplicates that did not seem to help in my case, but might be useful for someone else:对我来说似乎没有帮助但可能对其他人有用的潜在重复项:

EF6 Cascade Delete EF6 级联删除

Code First Cascade Delete代码优先级联删除

TPT Cascade Delete TPT级联删除

First of all you must include your navigation property in query explicitly.首先,您必须明确地在查询中包含您的导航属性。 Than for some reason RemoveRange doesn't work as expected with cascade delete, but if you iterate and remove one by one it works.由于某种原因, RemoveRange在级联删除时不能按预期工作,但是如果您迭代并一一删除它就可以了。

var products = Set<SpecialProduct>().Include(p => p.MyProperties).ToList();
products.ForEach(p => AllProducts.Remove(p));
SaveChanges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM