© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
17 replies
_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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Polymorphic relationships in EF Core / .NET Core
C#CC# / help
3y ago
✅ ASP.NET API (EF Core) Relationships
C#CC# / help
4y ago
EF Core
C#CC# / help
2y ago
✅ EF Seeding and Relationships
C#CC# / help
3y ago