Table-具有不同键名的每个类型数据模型?

Table-Per-Type data-model with different key names?

我有一个遗留数据库模式,我想用 table-per-type Entity Framework 映射来建模。然而,基础 table 和子类 table 对主键使用不同的名称,而 EF 似乎不喜欢这样。

例如:

[Table("People")]
abstract class Person {
    [Key, Column("Id")]
    public int Id { get; set; }

    // Person properties...
}

[Table("Employees")]
class Employee : Person {
    [Key, Column("PersonId")]
    public new int Id { get; set; }

    // Employee-specific properties...
}

这确实有效,至少对于阅读而言:Employee 映射到 People table 的记录加上 Employees [=32] 的记录=].但是,Entity Framework 不会填充 Employee.Id(它始终为零),因此我将 Employee.Id 注释为 [Obsolete] 以确保我的其余代码不会尝试使用它。

我担心以这种方式使用 new 看起来很糟糕。

还有更idiomatic/correct的方法吗?

好吧……我对此有一些解决方案……问题是为基 table 和派生 table 上的主键声明不同键名的唯一方法是在派生 class 中再次声明密钥,并且 EF 未在派生 class 上为密钥设置值,因此:

[Table("Employees")]
class Employee : Person {
    [Key, Column("PersonId")]
    public new int Id {get{return base.Id;}set{base.Id=value;}}
}

在派生 class 中声明密钥,但将其委托给基 class 中的密钥 属性。当 Entity Framework 设置一个时,它会自动设置两个——这很好,因为根据定义它们必须相同。