实体框架关联:错误,因为从属角色属性不是关键属性
使用实体框架 4.1
我有以下与实体框架一起使用的表
User ( UserId PK 身份、用户名、密码 )
医师(医师 ID 唯一身份,UserId 参考用户 PK、医师姓名)
PhysicianSite(SiteId、位置、PhysicianId FK 到 Physician 表)
问题。实体框架不允许我将 Physician 与 PhysicianSite 关联,因为 PhysicianId 不是 Physician 表上的主键。
用户是医生的基础,因为我的应用程序还有其他类型的用户,例如患者。
我收到以下错误
错误 2 错误 113:多重性在关系“PhysicianSitePhysician”中的角色“医生”中无效。由于从属角色属性不是关键属性,因此从属角色的重数上限必须为*。
有人可以告诉我如何在实体框架中创建 Physician 到 PhysicianSite 的关联,而无需 PhysicianId 成为 Physician 表上的主键吗?
Using Entity Framework 4.1
I have the following tables I'm using with Entity Framework
User ( UserId PK identity, UserName, Password )
Physician ( PhysicianId unique identity, UserId refereces User PK, PhysicianName )
PhysicianSite ( SiteId, Location, PhysicianId FK to Physician table)
Problem. Entity Framework would not allow me to associate Physician with PhysicianSite because PhysicianId is not the primary key on the Physician table.
User is the base of Physician because my application has other types Users, such as Patients.
I get the following error
Error 2 Error 113: Multiplicity is not valid in Role 'Physician' in relationship 'PhysicianSitePhysician'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *.
Could someone tell me how I can create the association with Physician to PhysicianSite in Entity Framework without PhysicianId being the primary key on the Physician table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实体框架需要外键链接到表的主键。但是,实体框架的主键不必与数据库中的主键匹配。如果您告诉 EF PhysicianId 是主键,它不会检查它是否确实是主键。它会很乐意接受 PhysicianId 的外键。
如果您有一些外键指向 PhysicianId,而其他外键指向真正的主键,那么您就不走运了,但看起来您并非如此。
Entity Framework requires foreign keys to link to a table's primary key. However, the primary key to Entity Framework does not have to match the primary key in the database. If you tell EF the PhysicianId is the primary key, it won't go checking if it really is. And it will happily accept foreign keys to the PhysicianId.
If you have some foreign keys to PhysicianId, and others to the real primary key, you're out of luck, but it doesn't seem like you do.
从 EDMX 中删除所有表并通过再次添加所有表来更新 edmx。如果不刷新所有导航属性,则会出现此类错误
Delete your all tables from EDMX and Update edmx by adding all tables again. Ef not refresh all navigation properties so it gives such errors
如果您有一个链接表,其中有一列具有标识规范,请将其删除(这会将链接表限制为该链接表的 ID 的唯一副本)。
If you have a link table with a column with identity specification, remove that (it would restrict the link table to only one copy of the ID for that linked table).