更新:此時我只能將一種產品分配給配方。我想要做的是添加對配方中 db 的所有產品的訪問(控制器創建) - 這里我使用 public int ProductId 但它只允許保存一個產品。我想從這個串列中選擇一些產品并將外鍵保存在資料庫中。照片
我也嘗試在 ProductId 中添加 public List < int > 但我從物體框架中得到錯誤。
我將不勝感激任何幫助。
public class Recipe
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int ProductId { get; set; }
public List<Product> Products { get; set; }
public Recipe()
{
this.Products = new List<Product>();
}
}
public class Product
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public Recipe? Recipes { get; set; }
}
uj5u.com熱心網友回復:
如果你想創建一個一對多的關系,你幾乎是在正確的方向,但你應該洗掉public int ProductId { get; set; }并重新排列,如下例所示。
假設您有以下課程:
public class Recipe
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public List<Product> Products { get; set; } = new();
}
public class Product
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public Recipe Recipe { get; set; }
}
您可以按以下方式實體化和使用:
public static void Main()
{
var recipe = new Recipe
{
Name = "My Recipe",
Products = new List<Product>
{
new Product { Name = "Product 1" },
new Product { Name = "Product 2" },
new Product { Name = "Product 3" }
}
};
recipe.Products.ForEach(product =>
{
Console.WriteLine(product.Name);
});
}
uj5u.com熱心網友回復:
聽起來您正在尋找多對多關系而不是一對多關系。
如果您使用的是 Code-First 和 EF6 或 EF Core 5 ,那么您可以使用:
public class Recipe
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; } = new List<Product>();
}
public class Product
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Recipe> Recipes { get; set; } = new List<Recipe>();
}
要了解幕后發生的事情,EF 應該創建一個名為 ProductRecipe 或 RecipeProduct 的連接表,其中包含兩個 FK。ProductId 和 RecipeId。這些也將形成該表的復合 PK。使用此表,EF 可以將一種產品與多個配方相關聯,同時還將一種配方與各種產品相關聯。在物件模型中,您可以獲得每個配方的產品集合,以及每個產品的配方集合。
在早期版本的 EF Core 中,您只能宣告此鏈接物體,因此您將擁有更多類似的內容:
public class Recipe
{
// ...
public virtual ICollection<ProductRecipe> ProductRecipes { get; set; } = new List<ProductRecipe>();
}
public class Product
{
// ...
public virtual ICollection<ProductRecipe> ProductRecipes { get; set; } = new List<ProductRecipe>();
}
public class ProductRecipe
{
[Key, Column(Order = 0)]
public int ProductId { get; set; }
[Key, Column(Order = 1)]
public int RecipeId { get; set; }
public virtual Product Product { get; set; }
public virtual Recipe Recipe { get; set; }
}
此方法在其他版本的 EF 中仍然是一個選項,如果您希望支持將任何其他欄位添加到聯接表,則需要此方法。例如,如果您想跟蹤 CreatedDate/ModifiedDate 等內容,以記錄產品何時與配方等相關聯。要通過 EF 將該資訊公開給應用程式,EF 需要了解 ProductRecipe 作為一個物體。權衡的是,當您通常關心食譜的產品等時,這種方法“更麻煩”。要獲取食譜的產品串列,您需要:
var products = context.Products
.Where(p => p.ProductRecipes.Any(pr => pr.RecipeId == recipeId)
.ToList();
或者
var products = context.Recipies
.Where(r => r.RecipeId == recipeId)
.SelectMany(r => r.ProductRecipies.Select(pr => pr.Product).ToList())
.ToList();
與第一種方法中的隱含連接表相比:
var produts = context.Recipes
.Where(r => r.RecipeId == recipeId)
.SelectMany(r => r.Products)
.ToList();
...這可以說更容易閱讀。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/459604.html
上一篇:如何為此方法引數傳遞文本框值?
