✅ Seeding question
Hi all,
In my app I am using EF Core
I have a many to many relationship between machine and option
Every machine has a different price per option, so I added price as payload to the junction table.
Pseudocode:
Machine:
Option:
OptionMachine:
Now my question is how do I seed Machine?
Lets say I have some existing options:
As you can see I cannot use the machine itself to seed the junction table, how do I go about doing this?
16 Replies
I know I can probably add them later, outside of the initialization but I'd like to enforce that a machine needs to exist WITH options WITH a price.
You do not have to set
Machine
in your OptionMachine
entity for this to work. Assuming your relationships aren't messed up, EF should handle this for you.
https://learn.microsoft.com/en-us/ef/core/saving/related-data#adding-a-graph-of-new-entities
I also don't know why you're not using auto props here
Gotcha thanks! One more question however:
As you can see in my machine class I have only a List of OptionMachine
But not a list of Option.
Online I always saw that they add both the explicit Junction/Join entity and the entity itself?:
https://learn.microsoft.com/en-us/ef/core/modeling/relationships/many-to-many#many-to-many-and-join-table-with-payload
https://stackoverflow.com/questions/78766403/net-ef-core-how-to-correctly-insert-and-update-a-model-in-a-many-to-many-rela
For example in above Post, they hold both a list of Tag and a list of PostTag
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...
Many-to-many relationships - EF Core
How to configure many-to-many relationships between entity types when using Entity Framework Core
Do you need both or is one fine?
Not sure, I always set my many-many relationships up to use an explicit join table
Okay thanks, do you use them both?
A list of your item and a list of your junction table?
For example this Player class has both, but I am not sure if its necesarry?:
No, I only use the explicit join table in my entities
There are probably cases where having access to the other nav collection would be useful, but I don't use it myself
Gotcha, so you only hold TeamPlayers in the image's case?
Yup
Great thanks, that's what I was wondering if it worked
Thanks 😛
:PepoSalute:
True, thats why I wanted to omit it
one thing to note, though, if you are creating a new entity and it references other entities that already exist, you may only need to set the ID on your join entity
say, for example, you are creating a new machine that references an existing option
i would probably do something like
I've had times where the EF change tracker tried re-creating the entities specified if the nav property was set to something that already existed
Oh I see thanks for the tip 🙂
I'll put that to good use 😛
:PepoSalute: gl
Thanks 😋