A one-to-many relationship connects one recipe with many ingredients. From the ingredient’s perspective, it is many-to-one: each ingredient belongs to one recipe. EF Core maps this relationship between application entities and database tables. section 12.1.4
Recognize the relationship through these parts:
- Recipe exposes an ICollection<Ingredient>, representing the many side. section 12.2.2
- Ingredient contains RecipeId, a foreign key pointing to the Recipe primary key. section 12.2.2
- EF Core uses the model to create the database structure and generate SQL for creating, reading, updating, and deleting entities. section 12.1.4
Recipe ingredients
A recipe such as fish pie can contain several ingredients, while each ingredient row belongs to that recipe. Identical ingredients in different recipes remain separate instances: changing lemon quantity in lemon chicken does not change fish pie. section 12.1.4
Listing 12.2 Defining the EF Core entity classes
public class Recipe
{
public int RecipeId { get; set; }
public required string Name { get; set; }
public TimeSpan TimeToCook { get; set; }
public bool IsDeleted { get; set; }
public required string Method { get; set; }
public required ICollection<Ingredient> Ingredients { get; set; } #1
}
public class Ingredient
{
public int IngredientId { get; set; }
public int RecipeId { get; set; }
public required string Name { get; set; }
public decimal Quantity { get; set; }
public required string Unit { get; set; }
}
Inspect the C# entity classes showing the collection navigation property and foreign-key property. section 12.2.2











