C
C#2y ago
_apix

❔ 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:
public class Coffee
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

[Required]
[StringLength(50)]
public string Name { get; set; }
[Required]
[StringLength(400)]
public string Description { get; set; }
public int Stock { get; set; }
}
public class Coffee
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

[Required]
[StringLength(50)]
public string Name { get; set; }
[Required]
[StringLength(400)]
public string Description { get; set; }
public int Stock { get; set; }
}
public class Characteristic
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

[Required]
[StringLength(50)]
public string Name { get; set; }
public int Stock { get; set; }
}
public class Characteristic
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

[Required]
[StringLength(50)]
public string Name { get; set; }
public int Stock { get; set; }
}
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
Shinyshark
Shinyshark2y ago
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
Angius
Angius2y ago
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
_apix
_apix2y ago
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...
Angius
Angius2y ago
Every kind of a drink would have a different ID, wym?
Shinyshark
Shinyshark2y ago
No his coffee, espresso and latte are individual drinks.
Angius
Angius2y ago
And I proposed a different structure
Shinyshark
Shinyshark2y ago
He wants a transactional history of what drink was ordered with what characteristics
Angius
Angius2y ago
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
Shinyshark
Shinyshark2y ago
@_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.
_apix
_apix2y ago
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.
Accord
Accord2y ago
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.