I have a design problem with regards to Entity Framework model relationship
I have this model in the edmx
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
The Problem : In order to get the Participant's Role value, I have to drill down through Participant->ParticipantRole->Role
(see JSON output below)
The Question:
In EF, how to design the table relationship to bypass the ParticipantsRole table. I want to access the Role in something like this Particant.Role
and not Participant.ParticipantsRole.Role
You say A Participant
can have multiple Role
s . And of course, a Role
can have multiple Participant
s. 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. You would have had Participant.Roles
and Role.Participants
as navigation properties.
However, the model has been generated with ParticipantsRole
and you want to get rid of it. (Or not, I'll get back to that).
This is what you can do:
ParticipantRoles
from the class diagram. ParticipantRoles
so it only has the two FK columns, that both form the primary key. ParticipantsRole
in the Add tab. 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). 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. - Something to consider...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.