_apix
_apix
CC#
Created by _apix on 1/24/2023 in #help
❔ 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.
18 replies