如何使EF6在关联/关系多重性中获得唯一约束(在FK上)? [英] How to get EF6 to honor Unique Constraint (on FK) in Association/Relationship multiplicity?
问题描述
这可能与我的其他问题 - 这似乎是:
This may be related to my other question - which seems to be that either:
-
实体框架是一个可怕的关系代数映射器 1 或;
Entity Framework is a terrible Relational Algebra mapper1 or;
(我希望的)我正在俯瞰SSDL / CSDL和EDMX模型或EF映射的一般。
(which I am hoping for) I am overlooking something with SSDL/CSDL and the EDMX model or EF mappings in general.
我有一个> Schema First 模型,模式如下所示:
I have a Schema First model and the schema looks like this:
ExternalMaps
---
emap_id - PK
Melds
---
meld_id - PK
emap_id - >>UNIQUE INDEX<< over not-null column, FK to ExternalMaps.emap_id
为了验证,这些脚本如下,其中应该导致多个 ExternalMaps:1> 0..1:Melds
2 。
For verification, these are scripted as the following, which should result in a multiplicity of ExternalMaps:1 <-> 0..1:Melds
2.
ALTER TABLE [dbo].[Melds] WITH CHECK ADD CONSTRAINT [FK_Melds_ExternalMaps]
FOREIGN KEY([emap_id]) REFERENCES [dbo].[ExternalMaps] ([emap_id])
CREATE UNIQUE NONCLUSTERED INDEX [IX_Melds] ON [dbo].[Melds] ([emap_id] ASC)
然而,当我使用EDMX设计人员从头开始从数据库(SQL Server 2012)进行更新,它不正确地创建了关联/外键关系为 ExternalMap:1< M:Meld
。
However, when I use the EDMX designer to update from the database (SQL Server 2012), from scratch, it incorrectly creates the Association / Foreign Key relation as ExternalMap:1 <-> M:Meld
.
当我尝试为Meld手动更改多重性时(通过设计器中的关联集属性)到 1
或 0..1
,我得到:
When I try to change the multiplicity manually for the Meld (via the "Association Set" properties in the designer) side to either 1
or 0..1
, I get:
运行转换:多重性在关系'FK_Melds_ExternalMaps'中的角色'Meld'中无效。因为从属角色属性不是关键属性,所以从属角色的多重性的上限必须是
*
。
(与我的另一个问题一样,这似乎与唯一约束相关,而不是正确注册/荣誉为候选键。)
(As with my other question, this seems to be related to Unique Constraints not being correctly registered/honored as Candidate Keys.)
如何让EF兑现 1< - > 0..1 / 1
多重性,由模型建立?
How can I get EF to honor the 1 <-> 0..1/1
multiplicity, as established by the model?
1 虽然我希望不是这样,但是当试图让EF映射到完美有效的RA模型时,我没有结束悲伤:LINQ to SQL(L2S)不是有这个问题。由于我的另一个问题并不是对这样一个受欢迎的ORM的回应,我对这个工具不信任。
1 While I hope this is not the case, I am having no end to grief when trying to get EF to map onto a perfectly valid RA model: LINQ to SQL (L2S) does not have this problem. Since my other question was not trivially answered for such a popular ORM, I am losing faith in this tooling.
2 FK不是另一种方式:虽然不可空的外键。 - 这也不是,因为它是一个共享PK,因为这个从2009年回答建议作为一个修复。
2 It is by design that the FK is not the other way: "Though shalt not have nullable foreign keys." - It is also not the case that it's a "shared" PK as this answer from 2009 suggests as a fix.
我正在使用EF 6.1.1,VS 2013 Ultimate,而不是将使用任何OO子类型功能 - 如果这样做有任何改变。
I am using EF 6.1.1, VS 2013 Ultimate, and am not going to use any "OO subtype features" - if that changes anything.
编辑
多重性无效,因为从属角色属性不是关键属性?(从2011年开始) - 2015年的EF微软认可的企业级ORM案例是否仍然是这种情况?
Multiplicity is not valid because the Dependent Role properties are not the key properties? (from 2011) - is this still the case for the EF "Microsoft-endorsed Enterprise-ready" ORM in 2014 2015?
在这个速度下,有人问为什么没有使用EF,我会有一个很大的原因,除了LINQ to SQL的工作很好..
At this rate the next time someone asks why EF wasn't used I'll have a large set of reasons other than "LINQ to SQL works just fine" ..
推荐答案
问题是实体框架(从EF4到EF6.1,谁知道多久)不了解唯一限制的概念和他们所暗示的一切:EF映射代码第一,而不是关系代数
The problem is that Entity Framework (from EF4 through EF6.1, and who knows how much longer) does not "understand" the notion of Unique Constraints and all that they imply: EF maps Code First, not Relational Algebra *sigh*
我的相关问题的这个答案提供了一个指向添加缺少功能的请求 ,并总结出来:
This answer for my related question provides a link to a request to add the missing functionality and sums it up:
..实体框架目前仅支持基于主键的参考约束,并且不具有唯一约束的概念。
这可以扩展到处理独特约束和候选键的所有领域,包括在这个问题中提出的多重性问题。
This can be expanded to pretty much all realms dealing with Unique Constraints and Candidate Keys, including the multiplicity issue brought up in this question.
如果EF的这个严重限制被公开讨论,我会很高兴做出众所周知,特别是当EF被吹捧以支持Schema First和/或替换L2S。从我的观点来看,EF围绕映射(和支持),只有Code First作为一流的公民。也许在另外4年..
I would be happy if this severe limitation of EF was discussed openly and made "well known", especially when EF is touted to support Schema First and/or replace L2S. From my viewpoint, EF is centered around mapping (and supporting) only Code First as a first-class citizen. Maybe in another 4 years ..
这篇关于如何使EF6在关联/关系多重性中获得唯一约束(在FK上)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!