如何在EF代码中首先在继承层次结构中映射标记类的外键属性

时间:2016-04-22 17:27:59

标签: c# entity-framework entity-framework-6

我使用的是EF 6.1.3,Code-First工作流程,并偶然发现了我似乎无法解决的映射问题。

我的Campaign对象有Campaign(1):TestRecipient(M)关系。我真的不需要从Campaign返回TestRecipient的对象引用,因此仅使用CampaignId,即外键。 TestRecipient及其兄弟SeedRecipient都来自抽象Recipient类,目前只是当前设计中的标记类。 这是类(缩写):

public class Campaign
{
    public Campaign(Guid id, string name)
    {
        this.Id = id;
        this.Name = name;
    }

    public Guid Id { get; private set; }

    public string Name { get; private set; }

    public virtual ICollection<TestRecipient> TestRecipients  { get; private set; }
    public virtual ICollection<SeedRecipient> SeedRecipients  { get; private set; }
}

public abstract class Recipient
{
    protected Recipient()
    {
        Id = Guid.NewGuid();
    }

    protected Recipient(Guid campaignId, string emailAddress) : this()
    {
        CampaignId = campaignId;
        EmailAddress = new EmailAddress(emailAddress);
    }

    public Guid Id { get; protected set; }
    public Guid CampaignId { get; protected set; }
    public EmailAddress EmailAddress { get; protected set;}
}

public class TestRecipient : Recipient
{
    private TestRecipient(){ }
    public TestRecipient(Guid campaignId, string emailAddress)
        :base(campaignId, emailAddress) { }

    //public Guid CampaignId  //If I move this property here then mapping works it out without problems
    //{ get; private set; }
}

public class SeedRecipient : Recipient
{
    private SeedRecipient() { }
    public SeedRecipient(Guid campaignId, string emailAddress)
                   : base(campaignId, emailAddress) { }
}

public class EmailAddress
{
    private EmailAddress() { }
    public EmailAddress(string emailAddress)  { this.Value = emailAddress; }
    public string Value { get; private set;}
}

Campaign将映射到自己的表,并且使用TPH策略将收件人({1}}和TestRecipient)映射到SeedRecipient表。以下是实体配置:

Recipient

当我运行public class CampaignContext : DbContext { public CampaignContext() { } public CampaignContext(string dbConfigName) : base(dbConfigName) { } public DbSet<Campaign> Campaigns { get; set; } public DbSet<Recipient> Recipients { get; set; } public DbSet<TestRecipient> TestRecipients { get; set; } public DbSet<SeedRecipient> SeedsRecipients { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Configurations.Add(new CampaignConfiguration()); modelBuilder.Entity<Recipient>() .Map(m => m.ToTable("Recipients")) .Map<TestRecipient>(m => { m.Requires("Type").HasValue("Test"); }) .Map<SeedRecipient>(m => m.Requires("Type").HasValue("Seed")); } } class CampaignConfiguration : EntityTypeConfiguration<Campaign> { public CampaignConfiguration() { ToTable("Campaigns"); HasKey(p => p.Id); Property(p => p.Id).HasColumnOrder(1); HasMany<TestRecipient>(p => p.TestRecipients).WithRequired().HasForeignKey(p => p.CampaignId); Property(p => p.Name) .HasColumnName("Name") .HasColumnType("nvarchar") .HasColumnOrder(2) .HasMaxLength(250) .IsRequired(); } } 命令时,出现以下错误: Add-Migration

这些类是基于DDD设计的Domain模型的一部分。我想我们会引入更多的基类来封装ID和审计字段。如果我将The foreign key component CampaignId is not a declared property on type TestRecipient. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.属性移动到派生类,则会按预期生成迁移。

为什么没有EF&#34;看&#34;继承的CampaignId属性?这是EF的限制吗?请告诉我TPH是否可以使用此层次结构和/或我做错了什么?

0 个答案:

没有答案