I'm trying to use table per hierarchy (TPH) with two classes inheriting from an abstract base:
public abstract class StatusLog : EntityBase
{
public StatusLog()
: base()
{
StatusChangeValidations = new List<StatusChangeValidation>();
}
public virtual List<StatusChangeValidation> StatusChangeValidations { get; set; }
public int StatusId { get; set; }
}
public class DisputeStatusLog : StatusLog
{
public virtual Dispute Dispute { get; set; }
}
public class StatementStatusLog : StatusLog
{
public virtual Statement Statement { get; set; }
}
I've mapped them like this:
public class StatusLogMap : EntityTypeConfiguration<StatusLog>
{
public StatusLogMap()
{
// Primary Key
this.HasKey(t => t.Id);
...
}
}
public class DisputeStatusLogMap : EntityTypeConfiguration<DisputeStatusLog>
{
public DisputeStatusLogMap()
{
// Relationships
this.HasRequired(t => t.Dispute)
.WithMany(t => t.StatusLogs)
.Map(m => m.MapKey("ObjectInstanceID"));
}
}
public class StatementStatusLogMap : EntityTypeConfiguration<StatementStatusLog>
{
public StatementStatusLogMap()
{
// Relationships
this.HasRequired(t => t.Statement)
.WithMany(t => t.StatusLogs)
.Map(m => m.MapKey("ObjectInstanceID"));
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<StatusLog>()
.Map<DisputeStatusLog>(e => e.Requires("ObjectID").HasValue("28"))
.Map<StatementStatusLog>(e => e.Requires("ObjectID").HasValue("111"));
}
I'd expected this to work, but it causes a validation exception immediately:
ObjectInstanceID: Name: Each property name in a type must be unique. Property name 'ObjectInstanceID' is already defined.
Adding a foreign key property for ObjectInstanceID and changing the mappings to use HasForeignKey instead of MapKey results in a slightly different error:
ObjectInstanceID: : There is no property with name 'ObjectInstanceID' defined in the type referred to by Role 'StatusLog'.
Is there something wrong with my mappings, or is this a scenario that Entity Framework doesn't support yet? I'm currently using the 9/23 nightly build of EF 6.01 as I'd read on the EF CodePlex site that it may have fixed this problem, but I had the exact same error with EF 6 RC1.
I know I can fix this by splitting these classes up into multiple tables and using TPT, but would really prefer to avoid this as it's an existing database.