I use the "table-per-type" hierarchy in my first code project (EF5). My derived classes override the default primary key name to clearly identify this relationship from a database perspective, for example:
modelBuilder.Entity<Chair>() .Property(x => x.Id) .HasColumnName("ProductId"); modelBuilder.Entity<Table>() .Property(x => x.Id) .HasColumnName("ProductId");
Using the following classes as an example:
[Table("Product")] public class Product { public int Id {get; set;} } [Table("Chair")] public class Chair : Product { } [Table("Table")] public class Table : Product { public virtual ICollection<Chair> Chairs {get; set;} }
Which causes the following error in the Table.Chairs property: The column identifier specified as part of this MSL does not exist in MetaDataWorkspace .
Which I understand as EF probably did not see that the PK of the Chair product was changed .. (and still assumes it is called "Id"). But I cannot understand how I instruct EF to use an alternate key.
thanks,
PS. Yes, I know, if I do not change the PK names, this will work ... but can this be done using the EF Fluent API?
Elmar source share