如何告诉EntityFramework 5.0两个实体之间有一对一关联? [英] How to tell EntityFramework 5.0 that there's a one-to-one association between two entities?

查看:194
本文介绍了如何告诉EntityFramework 5.0两个实体之间有一对一关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在设计视图中进入EDMX,并更新关联的基数(1- ,0 - 等),但是我想知道这样做是否正确



我有两张表,例如:

 用户
-----
ID


UserProfile
-------------
Id
UserId
Stuff

单个用户可能只有一个配置文件。因此,我已经在 UserProfile 表中设置了UNIQUE约束,而不是 UserId 列。另外, UserProfile.Id 被定义为引用 User.Id 的外键。



然而,在实体框架模型中,我得到以下内容:

  class User 
{
ICollection< UserProfile> UserProfiles {get;组;
}

起初,我有 UserProfile 表中没有任何主键,如下所示:

  UserProfile 
----- ---------
UserId
Stuff

但实体框架使它成为只读,因为它需要在每个要写入的表上有一个主键。所以,我在 UserProfile 表中也添加了一个 Id 列。



更新



我尝试设置用户表和设计器中我的EDMX中的 UserProfile 表。但是,我收到这个错误提示我应该把它改回原来是一对多关系。

 运行转换:关系'FK_UserBasicProfile_User'中的角色'UserBasicProfile'
中的多重性无效。因为从属角色
属性不是关键属性,因此从属角色的多重性
的上限必须为*。


解决方案

我找到了一种方法。那么,不是纯粹的1-1关系,而是一个1-0..1的关系,就是我以前的关系,因为所有的实际目的都是一对一的关系。



如果你像我的问题中描述的那样陷入这个陷阱,这里是你所做的:



1)删除 Id 表单中的 UserProfile 表。因此,不要给它一个单独的主键。



2)相反,将依赖表中的外键标记为自己的主键。在这种情况下,将 UserId 字段标记为 UserProfile 表的主键。



除了这两个,我假设,正如我在问题中概述的那样,您在权限用户表)和依赖表(本示例中的 UserProfile 表) / p>

所以,你的新表结构应该如下所示:

 用户
------
Id


UserProfile
-------------
UserId (外键,也是此UserProfile表的主键)
Stuff

实体框架然后会识别这个1-0..1关系。


I could go into the EDMX in design view and update the cardinality (1-, 0-, etc.) of associations, but I want to know if it is alright to do that in this scenario.

I have these two tables, let's say:

User
-----
Id


UserProfile
-------------
Id
UserId
Stuff

A single user may have just one profile. Therefore, I have set a UNIQUE constraint no the UserId column in the UserProfile table. Also, UserProfile.Id is defined as a foreign key that references User.Id.

However, in the Entity Framework model, I get the following:

class User
{
  ICollection<UserProfile> UserProfiles { get; set; }
}

At first, I had the UserProfile table without any primary key of its own like this:

UserProfile
--------------
UserId
Stuff

But EntityFramework made that read-only as it needed to have a primary key on every table that you want to be made writable. So, I put an Id column in the UserProfile table as well.

UPDATE

I tried setting the multiplicit/cardinality of the association between the User table and the UserProfile table in my EDMX in the designer. However, I get this error suggesting I should turn it back to what it was, i.e. a one-to-many relationship.

Running transformation: Multiplicity is not valid in Role 'UserBasicProfile' 
in relationship 'FK_UserBasicProfile_User'. Because the Dependent Role 
properties are not the key properties, the upper bound of the multiplicity 
of the Dependent Role must be *.

解决方案

I've found a way. Well, not purely a 1-1 relationship but a 1-0..1 relationship was what I was after, which, for all practical purposes is a one-to-one relationship.

If you ever fall into this trap like I described in my question, here's what you do:

1) Remove the Id field from the dependent table, in this case the UserProfile table. Therefore, do not give it a separate primary key.

2) Instead, mark the foreign key in the dependent table as its own primary key. In this case, mark the UserId field itself as the primary key of the UserProfile table.

In addition to these two, I assume, as I outlined in the question that you've got a primary and foreign key relationship between the authority (the User table in this case) and the dependent table (the UserProfile table in this example).

So, you new table structure should look like this:

User
------
Id


UserProfile
-------------
UserId (foreign key, and also the primary key of this UserProfile table)
Stuff

Entity Framework will then recognize this 1-0..1 relationship.

这篇关于如何告诉EntityFramework 5.0两个实体之间有一对一关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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