简体   繁体   English

如何在代码中首先启用TPT(每种类型的表)继承中的级联删除?

[英]How to enable cascade delete in TPT (table per type) inheritance in code first?

I'm using EF with Code first and TPT (Table per Type) inheritance. 我使用EF代码优先和TPT(每类型表)继承。 I have the following model: 我有以下型号:

public partial class AccountHolder
{
    public int AccountHolderId { get; set; }

    public virtual Address Detail { get; set; }  
    public virtual Nominee Nominee { get; set; }   
}

public partial class Nominee
{
    public int NomineeId { get; set; }             
}

public abstract class Address
{
    public int AddressId { get; set; }
    ...
}

public class PersonalDetail : Address
{
    public int PersonalDetailId { get; set; }
    ...
}

Fluent api : 流利的api:

        modelBuilder.Entity<AccountHolder>().HasOptional(p => p.Nominee)
                                            .WithRequired()
                                            .WillCascadeOnDelete();

According to this tutorial here is a polymorphic relationship between AccountHolder and Address. 根据本教程,这里是AccountHolder和Address之间的多态关系。 PersonalDetail inherit Address. PersonalDetail继承地址。 My issue is whenever i delete any AccountHolder i want that EF will take care of deleting its associated PersonalDetail information from Address and PersonalDetail table but currently this is not happing, can anybody please guid me how i can implement this behavior through fluent API or some other approach ? 我的问题是每当我删除任何AccountHolder我希望EF将负责从Address和PersonalDetail表中删除其关联的PersonalDetail信息,但目前这不是讨厌,任何人都可以告诉我如何通过流畅的API或其他一些方式实现此行为方法?

Edit: 编辑:

According to the posted answer when i am using this configuration : 根据我使用此配置时发布的答案:

        modelBuilder.Entity<AccountHolder>().HasOptional(p => p.Detail)
                                            .WithRequired()
                                            .WillCascadeOnDelete();

for enabling cascade delete that this is conflicting with existing 1 to 1 association between AccountHolder and Nominee. 启用级联删除,这与AccountHolder和Nominee之间现有的1对1关联冲突。 The exception is : 例外是:

Conflicting changes detected. 检测到冲突的更改。 This may happen when trying to insert multiple entities with the same key. 尝试使用相同的密钥插入多个实体时可能会发生这种情况。

Check out the WillCascadeOnDelete function when specifying the EntityTypeConfiguration 指定EntityTypeConfiguration时,请查看WillCascadeOnDelete函数

http://msdn.microsoft.com/en-us/library/gg679348(v=vs.103).aspx http://msdn.microsoft.com/en-us/library/gg679348(v=vs.103).aspx

In that example you posted in the question, they have: 在你在问题中发布的那个例子中,他们有:

modelBuilder.Entity<Customer>()
              .HasOptional(c => c.BillingAddress)
              .WithRequired();

Slap a WillCascadeOnDelete(true) at the end of that (or for whatever Entity you want to have cascade), and that should do it. 在结束时(或者对于你想要级联的任何实体)啪一下WillCascadeOnDelete(true),并且应该这样做。 Something like: 就像是:

modelBuilder.Entity<AccountHolder>().HasOptional(a => a.Address).WithRequired().WillCascadeOnDelete(true);

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

相关问题 无法按类型获取表(TPT)继承策略-代码优先迁移 - Cant get Table per Type (TPT) Inheritance Strategy - Code First Migration 实体框架中的级联删除(每类继承的表) - Cascade delete in entity framework ( table per type inheritance ) EntityFramework-每种类型的表(TPT)继承和与CodeFirst的映射关系 - EntityFramework - Table per Type (TPT) Inheritance and Mapping Relationships with CodeFirst EntityFramework、Azure ElasticScale 和 Table Per Type (TPT) 继承 - EntityFramework, Azure ElasticScale, and Table Per Type (TPT) Inheritance 如何使Entity Framework TPT继承为每种类型建立一个表 - How can i make Entity Framework TPT inheritance to build one table per each type EF 4.3 Code First:具有复合主键和外键的每类型表(TPT) - EF 4.3 Code First: Table per Type (TPT) with Composite Primary Key and Foreign Key 使用EF /代码优先/存储库模式/每种类型的表(TPT)时的良好编程原则? - Good programming principles while using EF/Code First/Repository Pattern/Table Per Type (TPT)? 代码首先使用继承级联删除 - Code first cascade delete using inheritance 首先使用EF代码进行继承-每个具体类型(TPC)的表 - Inheritance with EF Code First - Table per Concrete Type (TPC) 代码优先级联删除 - Code First Cascade Delete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM