0

使用TPH和WillCascadeOnDelete(true)時遇到問題。當我將值設置爲true時級聯刪除時,我的數據庫不會創建。異常消息爲下列之一:實體框架 - 代碼優先-TPH和WillCascadeOnDelete(true)

在介紹表「MembersProfiles」外鍵約束「FK_dbo.MembersProfiles_dbo.Contacts_ContactId」可能會導致循環或多個級聯路徑。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY約束。

爲了清楚這裏的事情是我的模型和用於它的映射。

public class MemberProfile 
{ 
    public Guid MemberProfileId { get; set; } 
    public ICollection<Contact> Contacts { get; set; } 
} 

public abstract class Contact 
{ 
    public Guid ContactId { get; set; } 
    public Guid AddressId { get; set; } 
    public Address Address { get; set; } 
} 

public class PersonContact : Contact 
{ 
    public string Profession { get; set; } 
    public string OrganizationName { get; set; } 
} 

public class OrganizationContact : Contact 
{ 
    public string SalesPhone { get; set; } 
    public string ServicePhone { get; set; } 
} 

public class ContactMap : EntityTypeConfiguration<Contact> 
    { 
     public ContactMap() 
     { 
      ToTable("Contacts"); 
      HasKey(c => c.ContactId); 

      Property(c => c.ContactId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
      Property(c => c.Name).IsRequired().HasMaxLength(50); 
      Property(c => c.Email).IsRequired().HasMaxLength(150); 
      Property(c => c.MobilePhone).IsRequired().HasMaxLength(15); 
      Property(c => c.Description).IsOptional().HasMaxLength(500); 
      Property(c => c.FixPhone).IsOptional().HasMaxLength(15); 
      Property(c => c.FaxNumber).IsOptional().HasMaxLength(15); 

      HasRequired(mp => mp.Address).WithMany().HasForeignKey(mp => mp.AddressId); 
      HasRequired(mp => mp.Link).WithMany().HasForeignKey(mp => mp.LinkId); 
      HasRequired(mp => mp.Image).WithMany().HasForeignKey(mp => mp.MediaId); 
     } 
    } 

public class PersonContactMap : EntityTypeConfiguration<PersonContact> 
{ 
     public PersonContactMap() 
     { 
      Property(pc => pc.Profession).IsOptional().HasMaxLength(150); 
      Property(pc => pc.OrganizationName).IsOptional().HasMaxLength(150); 

      Map(pc => pc.Requires("Discriminator").HasValue("PersonContact").HasColumnType("nvarchar(max)"));    } 
    } 

public class OrganizationContactMap : EntityTypeConfiguration<OrganizationContact> 
{ 
     public OrganizationContactMap() 
     { 
      Property(oc => oc.SalesPhone).IsOptional().HasMaxLength(15); 
      Property(oc => oc.ServicePhone).IsOptional().HasMaxLength(15); 

      Map(oc => oc.Requires("Discriminator").HasValue("OrganizationContact").HasColumnType("nvarchar(max)")); 
     } 
} 

public class MemberProfileMap : EntityTypeConfiguration<MemberProfile> 
{ 
     public MemberProfileMap() 
     { 
      ToTable("MembersProfiles"); 
      HasKey(mp => mp.MemberProfileId); 

      Property(mp => mp.MemberProfileId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
      Property(mp => mp.Name).IsRequired().HasMaxLength(50); 
      Property(mp => mp.DateOfBirth).IsRequired(); 
      Property(mp => mp.Email).IsRequired().HasMaxLength(150); 
      Property(mp => mp.MobilePhone).IsRequired().HasMaxLength(15); 
      Property(mp => mp.Summary).IsOptional(); 

      HasRequired(mp => mp.Address).WithMany().HasForeignKey(mp => mp.AddressId).WillCascadeOnDelete(true); 
      Property(mp => mp.AddressId).HasColumnName("AddressId"); 

      HasOptional(mp => mp.Media).WithMany().Map(mp => mp.MapKey(new[] { "MediaId" })).WillCascadeOnDelete(true); 
      HasOptional(mp => mp.Tags).WithMany().Map(mp => mp.MapKey(new[] { "TagId" })).WillCascadeOnDelete(true); 
      HasOptional(mp => mp.Contacts).WithMany().Map(mp => mp.MapKey(new[] { "ContactId" })).WillCascadeOnDelete(true); 
     } 
} 

不幸的是我不能意識到我在做什麼錯...所以任何線索將不勝感激。

P.S:我使用的EF 5.0代碼第一次

回答

0

看是否有此BlogPost helps.,另一種選擇,我會嘗試啓動一個加關係之一,看到它打破,而不是一下子堆積的一切。有時候,你可以像使用individual property mappings而不是Fluent API一樣使用。查看我上面提到的那個博客上的不同鏈接。

1

好的,找到了問題。我有兩個表引用地址表。在我的情況下,Contacts和MemberProfile都持有對Addresses表的引用,並且在這兩種情況下級聯刪除都已打開。一旦我關閉級聯刪除的關係之一,一切都很好。