[英]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.
希望不要太多。
We have a table Form, with a FK of PatGUID
to a Patient table with field PatGUID
. 我们有一个表格Form,带有
PatGUID
的FK到患者表格,其中包含字段PatGUID
。 The Patient table has a PatID
int KEY field. Patient表具有
PatID
int KEY字段。
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
. 此示例中需要更改的相关字段是
PatGUID
更改为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: 所以我需要的最终结果是:
Primary Key Table : Patient, Field: PatGUID
(renamed PatientGUID) 主要关键表 :患者,字段:
PatGUID
(更名为PatientGUID)
Foreign Key Table : Form, Field: PatGUID
(renamed PatientGUID) 外键表 :表单,字段:
PatGUID
(重命名为PatientGUID)
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: 这似乎不应该造成大问题,但是
Patient.PatGUID
组合不是主键,并且重命名为PatientGUID
的PatGUID
字段未启用WCF数据服务以正确创建具有适当引用的引用适当的选择/加入:
SELECT … FROM [dbo].[Form] AS [Extent1]
INNER JOIN [dbo].[Patient] AS [Extent2] ON [Extent1].[PatGUID] = [Extent2].[PatGUID]
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. EF还不支持主体密钥不是主键但其他列具有唯一键约束的关系。 It is on the feature request list but neither implemented nor on the road map for the next release (EF 6).
它位于功能请求列表中,但未在下一版本(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 7中),期望等待一年或更长时间,直到它准备好生产。
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
. 在您的特定模型中,EF根本无法识别
Form
和Patient
之间的任何关系,因为Patient.PatientID
被标记为[Key]
,而不是Patient.PatientGUID
,而EF将Form.PatientGUID
视为普通的标量属性,而不是FK Patient
。
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. 理论上,您可以伪造
Patient.PatientGUID
作为模型中的[Key]
属性,但如果您不是从数据库创建模型,或者从代码优先模型创建数据库,那么它就不是数据库中的主键。 ,如果您手动在模型和(现有)数据库之间进行映射。 But I am not sure if this wouldn't cause subtle problems anywhere else. 但我不确定这是否会在其他地方引起微妙的问题。
The alternative is to write manual join
statements in LINQ if you want to fetch Patients
and related Forms
. 另一种方法是,如果要获取
Patients
和相关Forms
可以在LINQ中编写手动join
语句。 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. 但是,缺点是你不会在
Patient
和Form
之间有导航属性 - 引用或集合 - 你不能使用诸如Form.Patient.SomePatientProperty
加载( Include
),延迟加载或舒适的“虚线路径语法”(如Form.Patient.SomePatientProperty
类的Form.Patient.SomePatientProperty
您的LINQ查询中的Form.Patient.SomePatientProperty
等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.