[英]EF Core - Problem adding data to many to many relationship
在解釋我的問題之前,我只想解釋一下我的代碼目標是什么。
我的目標是創建一個簡單的桌面應用程序,用於從選定的食譜創建食譜和購物清單。 所以我希望能夠創建彼此分開的成分和食譜,然后將一定數量的成分(我稱之為 RecipeIngredients)添加到食譜中。 RecipeIngredient 類包含有關成分的信息,還包含數量和度量(例如 kg)。
話雖如此,我的問題是我一直在嘗試將 RecipeIngredients 添加到我的食譜中。
我已經使用 EF Core 建立了一個數據庫,並且能夠單獨存儲成分和食譜。 但是,當我嘗試將 RecipeIngredient 存儲在配方中時,出現以下錯誤:未處理的異常。 System.NullReferenceException:對象引用未設置為對象的實例。
我認為我的問題是我不完全了解創建RecipeIngredient 對象需要輸入哪些數據,或者如何將Recipe 鏈接到RecipeIngredient。
我已經在下面發布了我的代碼,從 main 開始。 你們中的任何人都可以指出我正確的方向嗎? 這是我第一次使用 EF Core,也是我在 C# 中嘗試的第一個應用程序,所以我可能做錯了什么。
//Main
static void Main(string[] args)
{
SQLightRecipeRepository context = new SQLightRecipeRepository();
var recipe = context.GetRecipe(1); // I already added recipes to the database, so they exist.
var ingredient = context.GetIngredient(1); // Already added ingredients to the database, so they exist.
recipe.RecipeIngredients.Add(new RecipeIngredient() { Ingredient = ingredient, Measurement = Measurement.kg, Quantity = 2 });
context.Save();
}
//Repository
public class SQLightRecipeRepository : IRecipeRepository, IIngredientRepository, IRecipeIngredientRepository
{
private RecipeContext context = new RecipeContext();
//Recipe
public void DeleteRecipe(int recipeId)
{
Recipe recipe = context.Recipes.Find(recipeId);
context.Recipes.Remove(recipe);
}
public Recipe GetRecipe(int recipeId)
{
return context.Recipes.Find(recipeId);
}
public IEnumerable<Recipe> GetRecipes()
{
return context.Recipes.ToList();
}
public void InsertRecipe(Recipe recipe)
{
context.Recipes.Add(recipe);
}
public void UpdateRecipe(Recipe recipe)
{
context.Entry(recipe).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
}
public IEnumerable<RecipeIngredient> GetAllIngredients(int recipeId)
{
return context.Recipes.Find(recipeId).RecipeIngredients.ToList();
}
//Ingredient operations
public IEnumerable<Ingredient> GetIngredients()
{
return context.Ingredients.ToList();
}
public Ingredient GetIngredient(int ingredientId)
{
return context.Ingredients.Find(ingredientId);
}
public void InsertIngredient(Ingredient ingredient)
{
context.Ingredients.Add(ingredient);
}
public void DeleteIngredient(int ingredientId)
{
Ingredient ingredient = context.Ingredients.Find(ingredientId);
context.Ingredients.Remove(ingredient);
}
public void UpdateIngredient(Ingredient ingredient)
{
context.Entry(ingredient).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
}
public void Save()
{
context.SaveChanges();
}
public IEnumerable<RecipeIngredient> GetRecipeIngredients(int recipeId)
{
var ingredients = context.RecipeIngredients.Where(r => r.RecipeId == recipeId);
return ingredients.ToList();
}
public IEnumerable<RecipeIngredient> GetRecipeIngredients()
{
return context.RecipeIngredients;
}
public RecipeIngredient GetRecipeIngredient(int ingredientId, int recipeId)
{
return context.RecipeIngredients.Find(recipeId, ingredientId);
}
public void InsertRecipeIngredient(RecipeIngredient recipeIngredient)
{
context.RecipeIngredients.Add(recipeIngredient);
}
public void DeleteRecipeIngredient(RecipeIngredient recipeIngredient)
{
context.RecipeIngredients.Remove(recipeIngredient);
}
public void UpdateRecipeIngredient(RecipeIngredient recipeIngredient)
{
context.Entry(recipeIngredient).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
}
//DBContext (RecipeContext)
public class RecipeContext : DbContext
{
public DbSet<Recipe> Recipes { get; set; }
public DbSet<Ingredient> Ingredients { get; set; }
public DbSet<RecipeIngredient> RecipeIngredients { get; set; }
public string DbPath { get; }
public RecipeContext()
{
var folder = Environment.SpecialFolder.LocalApplicationData;
var path = Environment.GetFolderPath(folder);
DbPath = Path.Join(path, "recept.db");
}
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite($"Data source={DbPath}");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Recipe>()
.HasMany(r => r.Ingredient)
.WithMany(r => r.Recipe)
.UsingEntity<RecipeIngredient>(
j => j
.HasOne(ri => ri.Ingredient)
.WithMany(i => i.RecipeIngredients)
.HasForeignKey(ri => ri.IngredientId),
j => j
.HasOne(ri => ri.Recipe)
.WithMany(r => r.RecipeIngredients)
.HasForeignKey(ri => ri.RecipeId)
);
}
//Recipe, Ingredient and RecipeIngredient class
public class Recipe
{
public int RecipeId { get; set; }
public string Name { get; set; }
public List<RecipeIngredient> RecipeIngredients { get; set; }
public ICollection<Ingredient> Ingredient { get; set; }
}
public class Ingredient
{
public int IngredientId { get; set; }
public string Name { get; set; }
public List<RecipeIngredient> RecipeIngredients { get; set; }
public ICollection<Recipe> Recipe { get; set; }
}
public class RecipeIngredient
{
public int IngredientId { get; set; }
public Ingredient Ingredient { get; set; }
public int RecipeId { get; set; }
public Recipe Recipe { get; set; }
public int Quantity { get; set; }
}
解決了這個問題。
以錯誤的方式添加了 RecipeIngredients。
我將RecipeIngredients 添加到導致問題的Recipes 列表中。 它是通過參考配方和成分創建一個 Recipeingredient 對象,並將其添加到 DBset RecipeIngredient 來解決的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.