✅ EF Core Junction Tables with seperate columns
In EF Core:
I have entity X which has a list of Item Y
(This will let EF Core make a junction table to represent the many to many relationship)
Now as per my requirements I needed to add a Price to that junction table. So that the price only exists when and if there is a link between X and Y.
So I manually made the junction entity XY and added a single item of X and a single item of Y along with ints for their ID's and I added a decimal price.
Now my question is: Is this the correct way to add columns to Junction tables?
23 Replies
Pseudocode example:
X Entity Class:
Y Entity Class:
XY Entity Class:
What you're descrbing seems like a one-to-many relationship to me
Yes, it is, however I need an additional column in the junction table, as you can see I added Price. I don't know however if this is the right thing to do in EF Core
Why do you need a junction table at all?
Because it is a many to many relationship
Not if Y doesn't have a list of X it's not
My bad forgot to add it, let me edit it
Sorry about that
Does that look right now?
Yeah, in that case that's valid. You can reference this for more information https://learn.microsoft.com/en-us/ef/core/modeling/relationships/many-to-many#many-to-many-with-class-for-join-entity
Many-to-many relationships - EF Core
How to configure many-to-many relationships between entity types when using Entity Framework Core
Actually, nevermind, Y is not supposed to have that list. EF Core knows X has a List of Y thus it is many to many by default
Cool, I'll check that out
Thanks
Again, this describes a one-to-many relationship
One-to-many: X has a list of Y, Y has only one X
Many-to-many: X has a list of Y, Y has a list of X
You are right, I added this
Thats why it is many to many afterall
But thanks
I think the link will help me further
What are you representing with X and Y?
Is it actually a one-to-many or many-to-many relationship?
Its a many to many, X can have many Y and Y can have many X
What are X and Y
X => Machine
Y => Option
What kind of machines and options
A machine can have many options an option can be linked to many machines
Doesn't matter haha it's right don't worry 😅
Alllrighty
Thanks for the help 😉
Sure thing
Maybe one last thing I'd like to ask:
If you look at OP's Question:
https://stackoverflow.com/questions/78766403/net-ef-core-how-to-correctly-insert-and-update-a-model-in-a-many-to-many-rela
You can see he holds both the junction and other entity
Player:
Team:
In my case:
I only want them with the price, is it okay to leave out List<Option>
And just use the junction List<OptionMachine>?
Or would that mean I have to use entityconfiguration?
Stack Overflow
.NET EF Core : how to correctly insert and update a model in a many...
I am writing a .NET CRUD application with layered architecture with EF Core for ORM. I have two models (Players and Teams) stored in the database with a join table between them (Player in Team).
Th...
That should be fine.
Gotcha thanks 😉