ef-code-first - Entity Framework - 代码优先 - TPH 和 WillCascadeOnDelete(true)

标签 ef-code-first entity-framework-5 code-first entity-relationship

我在使用 TPH 和 WillCascadeOnDelete(true) 时遇到问题。当我将删除级联的值设置为 true 时,不会创建数据库。异常消息如下:

Introducing FOREIGN KEY constraint 'FK_dbo.MembersProfiles_dbo.Contacts_ContactId' on table 'MembersProfiles' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

为了弄清楚这里的事情是我的模型和用于它的映射。

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 Code First

最佳答案

看看这个 BlogPost helps. ,另一种选择是我会尝试开始逐一添加关系以查看它在哪里中断,而不是立即堆叠所有内容。有时,仅使用单独的属性映射而不是像您所做的那样使用Fluent API会有所帮助。查看我上面提到的博客上的不同链接。

关于ef-code-first - Entity Framework - 代码优先 - TPH 和 WillCascadeOnDelete(true),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14863866/

相关文章:

c# - 管理不同父实体中重复实体的一般方法

asp.net-mvc - 不支持关键字 : 'data source' .:使用 ObjectContext 和 LocalDB 的 EF 代码优先

c# - 由外键 ef 4.3 组成的代码优先组合键

c# - 为什么 Add-Migration 有时会创建重复的迁移?

entity-framework - 在代码优先 Entity Framework 中手动编辑数据库

asp.net - Buddy Class 中的必需属性不适用于 Entity Framework 5 和 ASP.NET

entity-framework - 如何在 EF 中使用/保留 POCO 对象关系?

c# - EF Code-first,插入时如何将主键插入另一个字段?

entity-framework-4.1 - 映射多对多关系

ef-code-first - EF Code First 向查询添加了额外的列,该列不再存在于模型中