I want to map two derived classes to two tables (TPT)
[Table("User")]
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
[Table("Employee")]
public class Employee : User
{
public int UserId { get; set; }
public string DeptName { get; set; }
}
The tables already exist (i.e. I can't modify the schema) and are defined like this:
Note that the column UserId
in table Employee
is both its PK and a FK to table User
, column Id
.
The DbContext is as straight as possible - no fluent mappings defined.
public class TestInheritContext : DbContext
{
public DbSet<User> Users { get; set; }
}
When trying to access the Users
collection, I get the following EF exception:
System.Data.SqlClient.SqlException: Invalid column name 'Id'.
Apparently, it tries to read column Id
from the table Employee
.
All examples of TPT that I have seen use the same PK column name in all the tables. For instance, this one.
How do I fix it?