实体框架一对一映射问题 [英] Entity Framework One-To-One Mapping Issues
问题描述
使用VS 2010 beta 2,ASP.NET MVC。
Using VS 2010 beta 2, ASP.NET MVC.
我尝试创建一个实体框架文件,并从我的数据库获取数据。
I tried to create an Entity framework file and got the data from my database.
关系中有一些问题,所以我开始调整一些事情,但是我一直遇到以下错误,简单的一对一关系
There were some issues with the relationships, so I started to tweak things around, but I kept getting the following error for simple one-to-one relationships
错误1错误113:多重性在关系'FK_UserProfiles_Users'中的角色'UserProfile'中无效。因为从属角色属性不是关键属性,因此从属角色的多重性的上限必须为*。 myEntities.edmx 2024
Error 1 Error 113: Multiplicity is not valid in Role 'UserProfile' in relationship 'FK_UserProfiles_Users'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *. myEntities.edmx 2024
我的用户表包含与其他表的其他多对多关系,但是当我尝试与其他表格形成一对一的关系,该错误弹出。
My Users table is consists of some other many-to-many relationships to other tables, but when I try to make a one-to-one relationship with other tables, that error pops up.
用户表
- UserID
- 用户名
- 电子邮件
等..
UserProfiles表
- UserProfileID
- UserID(用户表FK)
- 位置
- 生日
- UserProfileID
- UserID (FK for Users Table)
- Location
- Birthday
推荐答案
对于一对一的关系,EF希望表使用相同的主键。真的,如果这是一个真正的一对一,他们可能应该。所以在你的例子中,如果你将UserID作为UserProfiles表的主键,你的一对一就可以工作。
For one-to-one relationships, EF expects that the tables are using the same primary key. And really, if it's a true one-to-one they probably should. So in your example, if you make UserID the primary key on the UserProfiles table, your one-to-one will work.
这篇关于实体框架一对一映射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!