EF4.1:可能具有零或一至零或一(0..1至0..1)的关系吗? [英] EF4.1: Possible to have zero-or-one to zero-or-one (0..1 to 0..1) relationship?

查看:75
本文介绍了EF4.1:可能具有零或一至零或一(0..1至0..1)的关系吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 4.0与SQL Server 2008 R2.我试图代表0..1到0..1的关系,但我不断收到以下错误:

.NET 4.0 with SQL Server 2008 R2. I am trying to represent a 0..1 to 0..1 relationship and I keep getting the following error:

错误113:多重性与关系'{1}'中的角色'{0}'中的引用约束冲突.由于从属角色"中的所有属性都是不可为空的,因此主体角色"的多重性必须为"1".

Error 113: Multiplicity conflicts with the referential constraint in Role '{0}' in relationship '{1}'. Because all of the properties in Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.

即使从属实体中的属性可为空,我也会收到此消息.可以在实体框架中表示这种关系吗?

I get this message even when the properties in the dependent entity are nullable. Is it possible to represent this relationship in Entity Framework?

这种情况何时发生的示例(与是否进行良好的数据库设计无关)是当您自己存在一个请求时,一旦请求完成,结果就会与之相关联,以及该请求最终被删除,但结果全部保留.

An example of when this might occur (independently of whether it is good database design to do so) is when you have a request that exists by itself, a result that becomes associated with it once the request is completed, and when the request is eventually pruned but the results all remain.

推荐答案

不,这不可能,我怀疑它是否可以在SQL Server中使用.数据库关系要求一端成为依赖.这意味着它引用了主体端的主键(PK)-我们称之为外键(FK).如果我们谈论一对一关系,则必须将FK标记为唯一,以便从属表中只有一个记录可以引用主体表中的给定记录.在这种情况下,唯一有效的关系是0..1-1-其中委托人可以不存在依赖关系而存在,但是仅当与现有委托人相关时,依赖关系才可以存在,因为必须将其FK值设置为委托人的PK值.理论上FK可以为空,但它取决于数据库如何实现唯一约束的方式.如果数据库将null视为另一个唯一值,则只有一个相关记录可以将FK设置为​​null(我认为这是SQL Server的情况).

No it is not possible and I doubt it works in SQL server. Database relation requires that one end becomes dependent. It means that it references primary key (PK) of a principal end - we call this foreign key (FK). If we talk about one-to-one relation the FK must be marked as unique so that only one record in the dependent table can reference a given record from the principal table. The only valid relation in this case is 0..1 - 1 where principal can exist without dependent but the dependent can exist only when related to existing principal because its FK value must be set to PK value of the principal. Theoretically FK can be nullable but it depends on the way how database implements unique constraints. If the database counts null as another unique value only one dependent record can have FK set to null (I think this is a case of SQL server).

在EF中,这甚至更加复杂,因为EF不支持唯一约束,因此只有当从属实体中的FK也是其PK时,您才能建立一对一关系(=无法将其设置为null ).如果无法将FK设置为​​null,则不能将其设置为可为空,并且由于必须存在该主体实体,否则引用完整性将引发错误.

In EF this is even more complicated because EF doesn't support unique constraints and because of that you can build one-to-one relation only when FK in dependent entity is also its PK (= no way to set it to null). If you cannot set FK to null you cannot have it nullable and because of that principal entity must exists otherwise the referential integrity will throw error.

对您来说最好的解决方案是将Request作为主体,将Result作为从属.必须首先创建请求,并且该请求必须与Result一起保存在数据库中. Result必须具有与相应的Request相同的PK值(该列不能自动递增)(并且PK必须为FK到Request).

The best solution for you is considering Request as principal entity and Result as dependent. Request must be created first and it must be kept in the database as long as the Result. Result must have same PK value (the column cannot be auto incremented) as the corresponding Request (and PK must be FK to Request).

这篇关于EF4.1:可能具有零或一至零或一(0..1至0..1)的关系吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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