【发布时间】:2016-11-04 21:11:12
【问题描述】:
我想使用代码优先和流利的 API 基于 3 个实体创建 3 个表。我使用的是实体框架版本 6。连接表需要一个 3 列主键和附加列。
我的问题:如何使用代码优先和 C# Fluent API 来创建/映射 PatientTreatment 表的 3 列主键?谢谢。
连接表 { PatentId, TreatmentId, TreatmentDate } 的 3 列主键的详细信息。 PatentId 和 TreatmentId 的值是从其他 2 个实体(表)中获取的,而 TreatmentDate 的值是手动输入的(例如 C# 代码或 T-SQL 脚本,如调用 getdate() 函数)。
三个实体的详细信息:
public class Patient {
public long PatentId {get; set;} // database created using Identity
...
}
public class Treatment {
public long TreatmentId {get; set;} // database created using Identity
...
}
和连接表(实体)
public class PatientTreatment
{
public long PatentId {get; set;} // part of the primary key from the Patient entity
public long TreatmentId {get; set;} // part of the primary key from the Treatment entity
public DateTime TreatmentDate {get; set;} // part of the primary key but its value is from C# code or from T-SQL script, not from other entity (table)
// other fields ...
}
【问题讨论】:
标签: c# entity-framework ef-code-first many-to-many