Cześć, mam problem z seed'owaniem danych do bazy danych MySql. Uzywam Entityframeworkcore oraz PomeloMySql. Poniewaz lista musi posiadac primary key, nie moge uzyc zwyklej listy stringow, tylko musialem stworzyc klase ktora jest generykiem mojej listy. To z kolei rodzi ponizszy problem.
Moja encja wyglada tak:
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FoodIdea.API.Entities
{
public class Recipe
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public List<IngredientsClass> Ingredients { get; set; } = new List<IngredientsClass>();
public int PreparationTime { get; set; }
public bool IsHardToDo { get; set; }
public double TasteRate { get; set; }
public class IngredientsClass
{
[Key]
public int Id { get; set; }
public string Ingredient { get; set; } = null!;
}
[ForeignKey("FoodId")]
public Food? Food { get; set; }
public int FoodId { get; set; }
}
}
a seed tak:
modelBuilder.Entity<Recipe>()
.HasData(
new Recipe()
{
Id = 1,
FoodId = 1,
PreparationTime = 30,
Ingredients = {
new IngredientsClass() { Ingredient = "tomato", Id = 1 },
new IngredientsClass() { Ingredient = "salad", Id = 2 },
new IngredientsClass() { Ingredient = "pasta", Id = 3 },
new IngredientsClass() { Ingredient = "pizza", Id = 4 }},
IsHardToDo = false,
TasteRate = 8
}
Otrzymuje bład:
The seed entity for entity type 'Recipe' cannot be added because it has the navigation 'Ingredients' set.
Za jakiekolwiek sugestie będę wdzięczny