实体框架4.1 - 非键列的关系 [英] Entity Framework 4.1 - Relationships between non-key columns

查看:142
本文介绍了实体框架4.1 - 非键列的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有涉及2的实体,但传统的SQL模式基本上有2个键列在同一个表(而不是2列键:见下文)。我需要创建一个关系回到人造键列。有没有办法在实体声明做框架4.1?

I have 2 entities that are related, but the legacy sql schema essentially has 2 key columns for the same table (not a 2-column key: see below). I need to create a relationship back to the 'faux key' column. Is there a way to do this declaratively in Entity Framework 4.1?

Public Class Client
    Inherits ModelBase

    <Key(), Required()>
    Public Property ClientID As Decimal

    <Required(), StringLength(50)>
    Public Property ClientCode As String

    ........


Public Class ClientLocation
    Inherits ModelBase

    ........

    <Required(), StringLength(50)>
    Public Property ClientCode As String

    ........

    <ForeignKey("ClientCode")>
    Public Overridable Property Client As Clients.Client

和我收到的错误是:

*进行了模型生成过程中检测到一个或多个验证错误:   System.Data.Edm.EdmAssociationConstraint:所有的种类   在引用约束的从属角色属性必须是   同样作为主体作用相应的属性类型。   财产客户code'上的实体ClientLocation类型不   匹配的特性客户端ID类型的实体客户的   引用约束ClientLocation_Client'。*

*One or more validation errors were detected during model generation: System.Data.Edm.EdmAssociationConstraint: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'ClientCode' on entity 'ClientLocation' does not match the type of property 'ClientID' on entity 'Client' in the referential constraint 'ClientLocation_Client'.*

由于它认为我试图映射的 ClientLocation.Client code> Client.ClientID ,当我真的想地图 ClientLocation.Client code>客户端.Client code

Because it thinks I'm trying to map ClientLocation.ClientCode > Client.ClientID, when I am really trying to map ClientLocation.ClientCode > Client.ClientCode...

有什么想法?

谢谢!

推荐答案

实体框架要求的关系是建立在主表中整个主键和相应的列(外键)之间它的从属表。

Entity framework demands that relation is built between whole primary key in the principal table and corresponding columns (foreign key) it the dependent table.

这篇关于实体框架4.1 - 非键列的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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