简体   繁体   English

实体框架:一对多关系

[英]Entity Framework: One to Many relationship

I have a design problem with regards to Entity Framework model relationship 我有一个关于Entity Framework模型关系的设计问题

I have this model in the edmx 我在edmx中有这个模型 EF图

Business Rule: 业务规则:

A Participant can have multiple Roles so I create a relationship table ParticipantRoles that has 1-to-Many relationship on the Participant and the Role table Participant可以有多个Roles因此我创建了一个关系表ParticipantRoles ,它在ParticipantRole表上具有1对多的关系

The Problem : In order to get the Participant's Role value, I have to drill down through Participant->ParticipantRole->Role (see JSON output below) 问题 :为了获得参与者的角色价值,我必须深入了解Participant->ParticipantRole->Role (参见下面的JSON输出)

在此输入图像描述

The Question: 问题:

In EF, how to design the table relationship to bypass the ParticipantsRole table. 在EF中,如何设计表关系以绕过ParticipantsRole表。 I want to access the Role in something like this Particant.Role and not Participant.ParticipantsRole.Role 我想访问像Particant.Role这样的角色而不是Participant.ParticipantsRole.Role

You say A Participant can have multiple Role s . 你说Participant可以拥有多个Role And of course, a Role can have multiple Participant s. 当然, Role可以有多个Participant So basically this is a many-to-many association. 所以基本上这是一个多对多的关联。

Entity Framework will only map pure many-to-many associations (without connecting class) when the junction table only has two foreign keys. 当联结表只有两个外键时,实体框架将只映射纯多对多关联(没有连接类)。 In your case, if the table ParticipantsRole only would have had a primary key consisting of ParticipantId and RoleId at the time of generating the model the class ParticipantsRole would not have been created. 在您的情况下,如果表ParticipantsRole 在生成模型时只有一个由ParticipantIdRoleId组成的主键,则不会创建类ParticipantsRole You would have had Participant.Roles and Role.Participants as navigation properties. 您可以将Participant.RolesRole.Participants作为导航属性。

However, the model has been generated with ParticipantsRole and you want to get rid of it. 但是,该模型已使用ParticipantsRole生成,您想要摆脱它。 (Or not, I'll get back to that). (或者不,我会回到那个)。

This is what you can do: 这是你可以做的:

  • Remove ParticipantRoles from the class diagram. 从类图中删除ParticipantRoles
  • Modify the database table ParticipantRoles so it only has the two FK columns, that both form the primary key. 修改数据库表ParticipantRoles ,使其只有两个FK列,它们都构成主键。
  • Update the model from the database and select ParticipantsRole in the Add tab. 从数据库更新模型,然后在“添加”选项卡中选择ParticipantsRole

This should give you a model with a pure many-to-many association. 这应该为您提供一个纯粹的多对多关联的模型。

However, think twice before you do this. 但是,在执行此操作之前请三思而后行。 M2m associations have a way of evolving into 1-m-1association (as you've got now). M2m协会有一种演变为1-m-1关联的方式(正如你现在所做的那样)。 The reason is that sooner or later the need is felt to record data about the association, so the junction table must have more fields and stops being a pure junction table. 原因是迟早需要记录关于关联的数据,因此联结表必须具有更多字段并且不再是纯联结表。 In your case I can imagine that one day participant's roles must have a fixed order, or one marked as default. 在您的情况下,我可以想象一天参与者的角色必须具有固定的订单,或者标记为默认的一个。 It can be a major overhaul to change a m2m association into 1-m-1 in a production environment. 在生产环境中将m2m关联更改为1-m-1可能是一次重大改革。 - Something to consider... - 需要考虑的事情......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM