[英]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.