我有一个抽象基类,用于我定义的几个实体。其中一个派生实体实际上是另一个实体的非抽象基类。EF代码优先配置中的多级继承
下面这段代码:
public abstract class BaseReportEntry {
public int ReportEntryId { get; set;}
public int ReportBundleId { get; set; } //FK
public virtual ReportBundle ReportBunde { get; set; }
}
//A few different simple pocos like this one
public PerformanceReportEntry : BaseReportEntry {
public int PerformanceAbsolute { get; set; }
public double PerformanceRelative { get; set; }
}
//And one with a second level of inheritance
public ByPeriodPerformanceReportEntry : PerformanceReportEntry {
public string Period { get; set; }
}
我使用的是基础EntityTypeConfiguration
:
public class BaseReportEntryMap<TReportEntry> : EntityTypeConfiguration<TReportEntry>
where TReportEntry : BaseReportEntry
{
public BaseReportEntryMap()
{
this.HasKey(e => e.ReportEntryId);
this.HasRequired(e => e.ReportsBundle)
.WithMany()
.HasForeignKey(e => e.ReportsBundleId);
}
}
想必这工作得很好继承的一个层次,但引发以下错误的一个案例它有第二级别:
The foreign key component 'ReportsBundleId' is not a declared property on type 'ByPeriodPerformanceReportEntry'
public class ByPeriodPerformanceReportEntryMap : BaseReportEntryMap<ByPeriodPerformanceReportEntry>
{
public ByPeriodPerformanceReportEntryMap()
: base()
{
this.Property(e => e.Period).IsRequired();
this.Map(m =>
{
m.MapInheritedProperties();
m.ToTable("ByPeriodPerformanceReportEntries");
});
}
}
Here's ReportB ReportB如果需要的话
public class ReportsBundle
{
public int ReportsBundleId { get; set; }
public virtual ICollection<PerformanceReportEntry> PerformanceReportEntries{ get; set; }
public virtual ICollection<ByPeriodPerformanceReportEntry> ByPeriodPerformanceReportEntries{ get; set; }
}
感谢您的回复,并对缺乏清晰度感到抱歉。基本上,我正在模拟一个外部报告系统,它具有这两个独立的报告。我打算使用ByPeriodPerformanceReportEntry和PerformanceReportEntry作为单独的具体实体,它们基本上只有公共属性(PerformanceAbsolute,PerformanceAbsolute等等)。您认为您可以使用这些新信息更新您的代码吗?谢谢你的帮助 – parliament 2013-03-25 01:17:24
我想你的意思是让PerformanceReportEntry抽象,并为具体实现创建第三个类,如“ConsolidatedPerformanceReportEntry”。这会工作吗?这是最好的选择吗? – parliament 2013-03-25 01:19:05
@Vazgen:我在我的答案中添加了编辑部分。 – Slauma 2013-03-25 18:01:55