Entity Framework 代码优先:Table 拆分和自定义字段
Entity Framework Code First: Table Splitting and Custom Fields
首先让我说,如果可以的话,我会更改此数据库模型,但我们的 OTS 解决方案之一就是这样设计的,以适应多个客户并允许自定义配置。
就是说,我使用 EF 和代码优先方法,并希望创建多个实体以映射到一个 table,该 table 实际上用作自定义字段数据 Table。 table 的架构如下:
ID EmployeeID Category Field1 Field2 Field3
1 1 Education NYU B.A. 2008
2 1 Retirement 401k 200.00 2054
3 2 Education GWU M.A. 2003
4 2 Retirement Roth 140.00 2048
还有另一个 table 某处解释了 Field1、Field2、Field3 的含义(例如大学、学位、教育毕业年份和投资、双周供款、退休资格年份)。
目前的配置预计不会改变(对我们公司而言),所以我宁愿将这个自定义字段 Table 映射到两个独立的实体:EducationData 和 RetirementData,它们具有智能命名的属性,例如 "University", "Degree"...等等。而不是 "Field1"、"Field2"...等等
关于如何解决这个问题有什么建议吗?理想情况下,我想将其配置为 EF 模型的一部分,以便我可以查询新字段,但我愿意接受建议。
context.Employees.EducationData(x => x.University == "GWU");
而不是
context.Employees.CustomFieldData(x => x.Category == "Education" && x.Field1 == "GWU")
我想您可以尝试这样做的一种方法是使用 TPH:
创建一些类型
abstract class CustomFieldData
{
public int ID { get; set; }
public int EmployeeID { get; set; }
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
}
class EducationData : CustomFieldData
{
public string School {
get { return Field1; } // you could use custom conversion here
set { Field1 = value; }
// similar for other fields
}
class RetirementData : CustomFieldData
{
// similar to EducationData
}
自定义 mapping for TPH
modelBuilder.Entity<CustomFieldData>()
.Map<EducationData>(m => m.Requires("Category").HasValue("Education"))
.Map<RetirementData>(m => m.Requires("Category").HasValue("Retiremen"));
忽略从 FieldX 映射的派生 类 中的字段,因为您不能将多个属性映射到同一数据库列。
modelBuilder.Entity<EducationData>().Ignore(t => t.School);
// similar for other properties
首先让我说,如果可以的话,我会更改此数据库模型,但我们的 OTS 解决方案之一就是这样设计的,以适应多个客户并允许自定义配置。
就是说,我使用 EF 和代码优先方法,并希望创建多个实体以映射到一个 table,该 table 实际上用作自定义字段数据 Table。 table 的架构如下:
ID EmployeeID Category Field1 Field2 Field3
1 1 Education NYU B.A. 2008
2 1 Retirement 401k 200.00 2054
3 2 Education GWU M.A. 2003
4 2 Retirement Roth 140.00 2048
还有另一个 table 某处解释了 Field1、Field2、Field3 的含义(例如大学、学位、教育毕业年份和投资、双周供款、退休资格年份)。
目前的配置预计不会改变(对我们公司而言),所以我宁愿将这个自定义字段 Table 映射到两个独立的实体:EducationData 和 RetirementData,它们具有智能命名的属性,例如 "University", "Degree"...等等。而不是 "Field1"、"Field2"...等等
关于如何解决这个问题有什么建议吗?理想情况下,我想将其配置为 EF 模型的一部分,以便我可以查询新字段,但我愿意接受建议。
context.Employees.EducationData(x => x.University == "GWU");
而不是
context.Employees.CustomFieldData(x => x.Category == "Education" && x.Field1 == "GWU")
我想您可以尝试这样做的一种方法是使用 TPH:
创建一些类型
abstract class CustomFieldData { public int ID { get; set; } public int EmployeeID { get; set; } public string Field1 { get; set; } public string Field2 { get; set; } public string Field3 { get; set; } } class EducationData : CustomFieldData { public string School { get { return Field1; } // you could use custom conversion here set { Field1 = value; } // similar for other fields } class RetirementData : CustomFieldData { // similar to EducationData }
自定义 mapping for TPH
modelBuilder.Entity<CustomFieldData>() .Map<EducationData>(m => m.Requires("Category").HasValue("Education")) .Map<RetirementData>(m => m.Requires("Category").HasValue("Retiremen"));
忽略从 FieldX 映射的派生 类 中的字段,因为您不能将多个属性映射到同一数据库列。
modelBuilder.Entity<EducationData>().Ignore(t => t.School); // similar for other properties