❔ EF Core Code-first relationships
Hi there, I am trying to create a coffee vending machine application. But, I can't quite work the relationships around my head.
The project requires to have pre-defined coffees (espresso, macchiato, latte, etc.) so that the user would be able to pick from, and characteristics (sugar, milk, creamer, etc.) that can be added to a picked coffee.
I've created the following entities:
I want to be able to store the created coffees linked with the chosen characteristics in a separate table "SoldCoffes". To my understanding, I need to have one-to-many relationship, but I'm kind of stuck on this one.
Any help would be appreciated.
11 Replies
So if I understand correctly, you want to create a One-To-Many relationship where you save a sold coffee and all of its characteristics?
Like an Espresso with sugar, milk and creamer or a Latte with milk
I'd have a table with types, a table of characteristics, and a table of drinks where each has a type and a bunch of characteristics
1-n between drink and type, n-m between drink and characteristic
Yes, that's right
This would result in a junction table:
| DrinkId | CharacteristicId |
How would I be able to differentiate multiple drinks created with same Id? Or am I just not seeing it right...
Every kind of a drink would have a different ID, wym?
No his coffee, espresso and latte are individual drinks.
And I proposed a different structure
He wants a transactional history of what drink was ordered with what characteristics
Ah, well, in that case just make my
Drinks
table a History
table
One way or another, it should reference the drink and the modifiers@_apix If you want to store each individual sale of a coffee with its characteristics I would recommend you use that junction table but you add one additional column to keep the rows grouped. Perhaps on a SaleId that starts from 1 and goes up by 1 each time you sell another coffee.
Because you have one coffee with many characteristics, you won't know if you will have to insert 1, 2 or 3 rows.
so you'd have to track that SaleId yourself.
Thank you very much for the help guys. My concern was having 3 or 4 tables for this. But the fourth one is needed to keep track of created coffees.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.