实体框架TPH创建多个ID列

本文关键字:ID 创建 框架 TPH 实体 | 更新日期: 2023-09-27 18:25:17

我有以下内容:

class Employee {
 [Key]
 public int EmployeeID {get;set;}
 public List<TimeSensitiveData> JobTitle {get;set;}
 public List<TimeSensitiveData> Department {get;set;}
 public List<TimeSensitiveData> OfficeLocation {get;set;}
}
abstract class TimeSensitiveData {
 [Key]
 public int TimeSensitiveDataID {get;set;}
 public string Value1 {get;set;}
 public DateTime Date {get;set;}
}
class JobTitle : TimeSensitiveData {}
class Department : TimeSensitiveData {}
class OfficeLocation : TimeSensitiveData {}

种子方法看起来像这样:

seed () {
 List<TimeSeriesData> oflc = new List<TimeSeriesData>();
            TimeSeriesData oflc2 = new OfficeLocation();
            oflc2.Value1 = "New York";
            oflc.Add(oflc2); 
 List<TimeSeriesData> dpt = new List<TimeSeriesData>();
            TimeSeriesData dpt2 = new Department();
            dpt2.Value1 = "New York";
            dpt.Add(dpt2); 
  List<TimeSeriesData> jt = new List<TimeSeriesData>();
            TimeSeriesData jt2 = new JobTitle();
            jt2.Value1 = "New York";
            jt.Add(jt2);
 var Employees = new List<Employee> {
  new Employee{JobTitle=jt,Department=dpt,OfficeLocation=oflc},
 } 
}

当我在数据库中植入雇员时,它会创建一个雇员表和一个TimeSentiveData表。在TimeSensitiveData表中有一个Discriminator列(我知道它在做什么),但也有多个Employee_EmployeeID列。实际上,它似乎为每个扩展TimeSentiveData的复杂对象创建了一个。

所以TimeSensitiveData表看起来有点像这个

TimeSensitiveDataID  Value1  Date  Discriminator  Employee_EmployeeID Employee_EmployeeID1 Employee_EmployeeID2
1                    xx      date  JobTitle       NULL                NULL(or value)       NULL
2
etc..
etc..

我不知道为什么会发生这种事,有什么想法吗?

我应该在员工模型中列出Lists而不是TimeSensitiveData类型的列表吗?它们应该是具体的类类型列表吗?

实体框架TPH创建多个ID列

[InverseProperty]部分修复