在主表中创建与重命名字段和非主键的实体关系 [英] Creating entity relationship with renamed fields and non-primary key in primary table
问题描述
以下是我试图定义外键关系的两个部分表。
The following are two partial tables in which I am trying to define a foreign key relationship.
public class Form
{
[Key, Column("FormID")]
public System.Guid FormGUID { get; set; }
[Column("PatGUID")]
public Nullable<System.Guid> PatientGUID { get; set; }
}
public class Patient
{
[Column("PatGUID")]
public System.Guid PatientGUID { get; set; }
[Key, Column("PatID")]
public int PatientID { get; set; }
}
除了这个例子的相关信息,领域,导航等外;希望不会太多。
I've eliminated all but the relevant information, fields, navigations, etc. for this example; hopefully not too much.
我们有一个表格,其中FK为 PatGUID
PatGUID
。
患者表有一个 PatID
int KEY字段。
We have a table Form, with a FK of PatGUID
to a Patient table with field PatGUID
.
The Patient table has a PatID
int KEY field.
我们有要求将我们的字段重命名我们的代码第一实体模型;此示例中需要更改的相关字段为 PatGUID
更改为 PatientGUID
。
We have requirements to rename our fields for our code first entity models; the relevant fields in this example needing changed is PatGUID
being changed to PatientGUID
.
我遇到的困难是尝试使用注释或流利来定义这个外键。
The difficulty I am having is trying to define this foreign key using either annotations or fluent.
所以我需要的最终结果是:
So the end result I need is:
-
主键表:病人,字段:
PatGUID
(重命名为PatientGUID)
Primary Key Table: Patient, Field:
PatGUID
(renamed PatientGUID)
外键表:表单,字段: PatGUID
(重命名为PatientGUID)
Foreign Key Table: Form, Field: PatGUID
(renamed PatientGUID)
这似乎不应该是一个大问题, Patient.PatGUID
不是主键,而是将 PatGUID
字段重命名为 PatientGUID
尚未启用WCF数据服务以适当的参考创建引用,因此正确选择/加入:
This doesn’t seem like it should pose a large problem but with the combination of Patient.PatGUID
not being the primary key and the PatGUID
fields being renamed to PatientGUID
has not enabled the WCF Data Service to properly create a reference with a proper reference thus a proper select/join of:
SELECT … FROM [dbo].[Form] AS [Extent1]
INNER JOIN [dbo].[Patient] AS [Extent2] ON [Extent1].[PatGUID] = [Extent2].[PatGUID]
推荐答案
EF不支持主体的键不是主键的关系,而是具有唯一键约束的其他列。它是在功能要求列表上,但是在下一个版本(EF 6)的路线图上都没有实现。如果它实现了(在EF 7可能),期望等待一年或更长时间,直到它准备生产。
EF doesn't yet support relationships where the principal's key is not the primary key but some other column with a unique key constraint. It is on the feature request list but neither implemented nor on the road map for the next release (EF 6). If it gets implemented at all (in EF 7 maybe) expect to wait a year or more until it's ready for production.
在您的特定型号中,EF不承认任何 Form
和患者之间的关系,因为
Patient.PatientID
被标记为 [Key]
,而不是 Patient.PatientGUID
,而EF会将 Form.PatientGUID
作为普通的标量属性,不是作为FK到患者
。
In your particular model EF doesn't recognize any relationship between Form
and Patient
at all because Patient.PatientID
is marked as [Key]
, not Patient.PatientGUID
, and EF treats Form.PatientGUID
as an ordinary scalar property, not as an FK to Patient
.
理论上您可以在模型中伪造 Patient.PatientGUID
作为模型中的 [Key]
属性,但它不是数据库,如果您不从代码优先模型从数据库或数据库创建模型,即手动在模型和(现有)数据库之间进行映射。但是我不知道这不会在别的地方产生微妙的问题。
In theory you could fake Patient.PatientGUID
as the [Key]
property in the model although it is not the primary key in the database if you don't create the model from the database or the database from a code-first model, that is, if you map between model and (existing) database manually. But I am not sure if this wouldn't cause subtle problems anywhere else.
另一种方法是写手册 join
LINQ中的语句,如果要获取患者
和相关的表单
。然后,您可以使用任意属性连接两个实体,而不仅仅是关键属性。在我看来,这是更干净,更棘手的做法。但是,不利之处在于您不会在患者
和表单
之间导航属性 - 引用或集合不能使用喜欢加载的功能( Include
),延迟加载或舒适的点路径语法(如 Form.Patient.SomePatientProperty
等)在您的LINQ查询。
The alternative is to write manual join
statements in LINQ if you want to fetch Patients
and related Forms
. You can then join two entities using arbitrary properties, not only key properties. This is, in my opinion, the cleaner and less "tricky" approach. However, the downside is that you won't have navigation properties - references or collections - between Patient
and Form
and you can't use features like eager loading (Include
), lazy loading or comfortable "dotted path syntax" (like Form.Patient.SomePatientProperty
, etc.) in your LINQ queries.
这篇关于在主表中创建与重命名字段和非主键的实体关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!