Code First: TPT Inheritance - Specify a different name for the primary key column in each table

2 answers

TPT , , , .
, .

public class BaseEntity
{
  public int Id { get; set; }    
}

public abstract class SubEntity : BaseEntity
{
  public BaseId
  {
    get => Id;
    set => Id = value;
  }
} 

NotMapped, , LINQ.

0

. :

public partial class Person
{
    // Any other PK name can thrown an exception
    public int ID { get; set; }
}

public partial class Employee : Person
{
    // Hide base class ID
    private new int ID { get; set }

    // Define derived class ID (that wrapped inherited ID)
    [NotMapped]
    public int EmployeeID
    {
        get { return base.PersonID; }
        set { base.PersonID = value; }
    }
}

( API) :

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Employee>()
        .Property(e => e.ID)
        .HasColumnName("EmployeeID");
}
-1

All Articles