与重命名字段和在主表中的非主密钥生成实体关系 [英] Creating entity relationship with renamed fields and non-primary key in primary table

查看:108
本文介绍了与重命名字段和在主表中的非主密钥生成实体关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是其中我试图定义一个外键关系两个部分表。

 公共类表
{
    [键,列(FormID)]
    公众的System.Guid FormGUID {搞定;组; }    [专栏(PatGUID)]
    公众可空<&的System.Guid GT; PatientGUID {搞定;组; }
}公共类病人
{
    [专栏(PatGUID)]
    公众的System.Guid PatientGUID {搞定;组; }    [键,列(PatID)]
    公众诠释PatientID {搞定;组; }

}

我已经消除了所有,但相关信息,字段,导航等,在这个例子中,希望不会太大。

我们有一个表的形式,与 PatGUID 的FK给病人表字段 PatGUID
病人表有一个 PatID INT重点领域。

我们必须要求我们重命名字段我们code第一款实体;在这个例子中,相关领域·需要修改为 PatGUID 被更改为 PatientGUID

我有尝试使用任何注释或定义这个外键的困难流畅。

所以,最后的结果我需要的是:


  • 主键表:患者,字段: PatGUID (改名PatientGUID)


  • 外键表:表单,字段: PatGUID (改名PatientGUID)


这似乎并不像它应该带来一个大问题,但随着组合 Patient.PatGUID 不是主键和 PatGUID 字段被重命名为 PatientGUID 尚未启用WCF数据服务正常创建这样一个适当的参考基准适当选择/加入:

  SELECT ... FROM [DBO]。[表] AS [Extent1]
INNER JOIN [DBO]。[患者] AS [Extent2] ON [Extent1]。[PatGUID] = [Extent2]。[PatGUID]


解决方案

EF还不支持的关系,其中主要的关键是不是主键,但具有独特的键约束一些列。这是<一个href=\"http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1050579-unique-constraint-i-e-candidate-key-support\">on该功能要求列表但既不执行,也不是路线图的下一个版本(EF 6)。如果它能够在所有实施(在EF 7也许)期望等待一年或更长时间,直到它为生产做好准备。

在您的特定型号EF不承认表格病人,因为在所有<$ C之间的任何关系$ C> Patient.PatientID 标记为 [重点] ,而不是 Patient.PatientGUID 和EF治疗 Form.PatientGUID 作为一个普通的标量属性,而不是作为一个FK到患者

在理论上可以伪造 Patient.PatientGUID 在模型中的 [重点] 属性虽然它不是如果您没有创建从数据库模型或从code-第一个模型数据库的数据库主键,也就是说,如果手动模式和(现有的)数据库之间进行映射。但我不知道这会不会造成微妙的问题在其他地方。

另一种方法是编写手册加入,如果你想获取在LINQ语句患者及相关表格。然后,您可以加入使用任意属性,不仅关键属性两个实体。这一点,在我看来,清洁,少狡猾的方式。不过,缺点是,你不会有导航属性 - 患者表格与您之间 - 引用或集合不能使用像预先加载功能(包含),延迟加载或舒适的虚线路径语法(如 Form.Patient.SomePatientProperty 等)。

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. The Patient table has a PatID int KEY field.

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:

  • Primary Key Table: Patient, Field: PatGUID (renamed PatientGUID)

  • Foreign Key Table: Form, Field: PatGUID (renamed 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:

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. 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.

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.

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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆