如何将EF6关联添加到不是主键的候选键/唯一键? [英] How to add an EF6 Association to a Candidate Key / Unique Key which is not the Primary Key?

查看:800
本文介绍了如何将EF6关联添加到不是主键的候选键/唯一键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Schema First ,我有一个数据库结构,因此

Using Schema First, I have a database structure, as so

ExternalDataItems
---
edataitem_id   PK -- surrogate auto-increment - NOT for FK relation here
datahash       UX -- Candidate Key / Unique Index (binary(20))

ExternalMaps
---
emap_id        PK
ext_datahash   FK on ExternalDataItems.datahash - NOT referencing the surrogate PK

,并在生成SSDL / CSDL 1 后,这个

and after generating the SSDL/CSDL1 has this

    <Association Name="FK_ExtMaps_ExtDataItems">
      <End Multiplicity="1" Role="ExternalDataItems" Type="Store.ExternalDataItems" />
      <End Multiplicity="*" Role="ExternalMaps" Type="Store.ExternalMaps" />
      <ReferentialConstraint> <!-- error on this element -->
        <Principal Role="ExternalDataItems">
          <PropertyRef Name="datahash" />
        </Principal>
        <Dependent Role="ExternalMaps">
          <PropertyRef Name="ext_datahash" />
        </Dependent>
      </ReferentialConstraint>
    </Association>

< RefferentialConstraint> 元素


运行转换:Principal Role引用的属性ExternalDataItems必须与EntityType ExternalDataItem的关键字完全相同,关系FK_ExtMaps_ExtDataItems的关系约束中的主要角色。确保所有关键属性都在主体角色中指定。

Running transformation: Properties referred by the Principal Role ExternalDataItems must be exactly identical to the key of the EntityType ExternalDataItem referred to by the Principal Role in the relationship constraint for Relationship FK_ExtMaps_ExtDataItems. Make sure all the key properties are specified in the Principal Role.

ExternalDataItems的主要角色(?)SSDL看起来像以下,对于PK,并且UX似乎不存在 2 ,除了作为简单的标量属性:

The "Principal Role" (?) for ExternalDataItems SSDL looks like the following, for the PK, and the UX does not appear to be present2, except as a simple scalar property:

  <EntityType Name="ExternalDataItems">
      <Key>
        <PropertyRef Name="edataitem_id" />
      </Key>
      ..
      <Property Name="datahash" Type="binary" MaxLength="20" Nullable="false" />
  </EntityType>

如何将此关系 - 使用FK添加到非PK候选键? (之后这个工作我也想要一个导航属性到CSDL。)

How can I add this Relation - using a FK to a non-PK Candidate Key? (After this "works" I'll also want to also have a Navigation Property to the CSDL.)

此外,关联行不会出现在我怀疑的设计表面只是从这个错误中散发出来的。我正在使用Entity Framework 6.1.1版(最新发布于nuget)和Visual Studio 2013 Ultimate Update 4。

Furthermore, the association line does not appear in the design surface - which I suspect is just fallout from this error. I am using Entity Framework version 6.1.1 (latest published on nuget) and Visual Studio 2013 Ultimate Update 4.

< sup> 1 标准EDMX从数据库更新似乎没有拿起这些FK关系(其中可能与此错误有关),上述结果是在使用Huagati DBML / EDMX工具之后。如果我尝试添加关联,则设计人员将错误地尝试使用主键(不受任何FK关系支持),并且没有提供选择替代属性的选项。

1The standard EDMX "Update from Database" didn't appear to pick up these FK relations (which may be related to this bug) and the results above are after using the Huagati DBML/EDMX tooling. If I tried to "Add Association" prior the designer would incorrectly try and use the primary key - which is not supported by any FK relation - and did not provide options for choosing alternative properties.

2 尝试添加< UniqueConstraint> 元素,如唯一约束在实体框架导致友好的XML验证错误:

2Attempting to add a <UniqueConstraint> element as described in "Unique Constraints in the Entity Framework" results in the friendly XML validation error:


元素'ElementType'..有一个无效的子元素' UniqueConstraint'。

The element 'ElementType' .. has an invalid child element 'UniqueConstraint'.


推荐答案

看起来像缺少重要功能


..实体框架当前仅支持基于主键的参考约束,并且不具有唯一约束的概念

您可以删除DB方案中的外键,并使用LINQ连接表:

You can remove foreign key in DB scheme and use LINQ to join tables:

from item in ExternalDataItems
join map in ExternalMaps on item.datahash = map.ext_datahash
select new { item.edataitem_id, map.emap_id };

此外,您还可以使用这些连接的表创建VIEW并使用该视图。

Also you can create the VIEW with these joined tables and use the one.

这篇关于如何将EF6关联添加到不是主键的候选键/唯一键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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