C
C#15h ago
Jiry_XD

✅ 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
Jiry_XD
Jiry_XDOP15h ago
Pseudocode example: X Entity Class:
List<Y> YList;
// Other attributes
List<Y> YList;
// Other attributes
Y Entity Class:
//General attributes for Y.
//General attributes for Y.
XY Entity Class:
X X
Y Y
decimal Price
X X
Y Y
decimal Price
mg
mg14h ago
What you're descrbing seems like a one-to-many relationship to me
Jiry_XD
Jiry_XDOP14h ago
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
mg
mg14h ago
Why do you need a junction table at all?
Jiry_XD
Jiry_XDOP14h ago
Because it is a many to many relationship
mg
mg14h ago
Not if Y doesn't have a list of X it's not
Jiry_XD
Jiry_XDOP14h ago
My bad forgot to add it, let me edit it Sorry about that Does that look right now?
mg
mg14h ago
Many-to-many relationships - EF Core
How to configure many-to-many relationships between entity types when using Entity Framework Core
Jiry_XD
Jiry_XDOP14h ago
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
mg
mg14h ago
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
Jiry_XD
Jiry_XDOP14h ago
You are right, I added this
builder.HasMany(x => x.Y)
.WithMany();
builder.HasMany(x => x.Y)
.WithMany();
Thats why it is many to many afterall But thanks I think the link will help me further
mg
mg14h ago
What are you representing with X and Y? Is it actually a one-to-many or many-to-many relationship?
Jiry_XD
Jiry_XDOP14h ago
Its a many to many, X can have many Y and Y can have many X
mg
mg14h ago
What are X and Y
Jiry_XD
Jiry_XDOP14h ago
X => Machine Y => Option
mg
mg14h ago
What kind of machines and options
Jiry_XD
Jiry_XDOP14h ago
A machine can have many options an option can be linked to many machines Doesn't matter haha it's right don't worry 😅
mg
mg14h ago
Alllrighty
Jiry_XD
Jiry_XDOP14h ago
Thanks for the help 😉
mg
mg14h ago
Sure thing
Jiry_XD
Jiry_XDOP14h ago
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:
public ICollection<Team> Teams { get; set; } = [];
public ICollection<TeamPlayer> TeamPlayers { get; set; } = [];
public ICollection<Team> Teams { get; set; } = [];
public ICollection<TeamPlayer> TeamPlayers { get; set; } = [];
Team:
public ICollection<Player> Players { get; set; } = [];
public ICollection<TeamPlayer> TeamPlayers { get; set; } = [];
public ICollection<Player> Players { get; set; } = [];
public ICollection<TeamPlayer> TeamPlayers { get; set; } = [];
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...
mg
mg14h ago
That should be fine.
Jiry_XD
Jiry_XDOP14h ago
Gotcha thanks 😉
Want results from more Discord servers?
Add your server