我根据一个教程创建了所有实体,并且似乎可以正常工作,至少创建了我的数据库表,但是我无法弄清楚如何使用以下设置插入任何新数据:
我的模型课:
public class Coffee
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CoffeeId { get; set; }
[MaxLength(100)]
[Required]
public string Name { get; set; }
[MaxLength(100)]
[Required]
public string ImagePath { get; set; }
[MaxLength(100)]
[Required]
public string Price { get; set; }
[MaxLength(400)]
[Required]
public string Description { get; set; }
public virtual ICollection<IngredientCoffees> Ingredients { get; set; }
}
public class Ingredient
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int IngredientId { get; set; }
[MaxLength(100)]
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
public string Name { get; set; }
[Required]
public int Amount { get; set; }
[Required]
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 1)]
public string Unit { get; set; }
public virtual ICollection<IngredientCoffees> Coffees { get; set; }
}
public class IngredientCoffees
{
public int Coffee_Id { get; set; }
public int Ingredient_Id { get; set; }
public virtual Coffee Coffee { get; set; }
public virtual Ingredient Ingredient { get; set; }
public int amount { get; set; }
}
在我的OnModelCreating
类的DbContext
方法中,我有这个:
builder.Entity<IngredientCoffees>()
.HasKey(ic => new { ic.Coffee_Id, ic.Ingredient_Id });
builder.Entity<IngredientCoffees>()
.HasRequired(ic => ic.Coffee)
.WithMany(ic => ic.Ingredients)
.HasForeignKey(ic => ic.Coffee_Id);
builder.Entity<IngredientCoffees>()
.HasRequired(ic => ic.Ingredient)
.WithMany(ic => ic.Coffees)
.HasForeignKey(ic => ic.Ingredient_Id);
所有这些都将导致以下数据库结构:
如何在数据库中插入具有现有成分的新Coffee
,使它们加入我的IngredientCoffees
表中,我还想在其中将“金额”列设置为它们? >
答案 0 :(得分:0)
您可以使用数组或列表
if (context.Coffee .Any())
{
return; // DB has been seeded
}
var coffees = new Coffee []
{
new Coffee { Name = "Coffee name", ImagePath = "/[your path]",
Price = "[price]", Description ="description" },
new Coffee {......................}
}
foreach (Coffee coffee in coffees)
{
context.Coffee.Add(coffee);
}
context.SaveChanges();
您可以在同一文件中对成分进行相同操作
var ingredients= new Ingredient []
{
new Ingredient { Name = "Ingredient name",
Amount = [your amount], Unit = "[ your unit ]" },
new Ingredient {......................}
}
foreach (Ingredient ingredient in ingredients)
{
context.Ingredient.Add(ingredient);
}
context.SaveChanges();
答案 1 :(得分:0)
我已经找到了解决我所遇到问题的方法!似乎EF在您想要拥有自定义列(在我的情况下为amount
)中的情况下对待很多很多关系,因此您必须做更多的手工工作,至少这是我读到的。
这是我的方法:
//create a new coffee
var americano = new Coffee
{
Name = "Americano",
ImagePath = "https://imgurl.jpg",
Price ="5",
Description = "Americano is a type of coffee drink prepared by.."
};
//select existing ingredient from the db
var coffee_powder = context.Ingredients.Where(i => i.Name == "coffee powder")
.SingleOrDefault();
//if found, create a new IngredientCoffee
if (coffee_powder != null) {
var ingredientCoffees = new IngredientCoffees { Coffee = americano, Ingredient = coffee_powder, amount = 8 };
//add it, with this your new coffee will be created automatically too
context.IngredientCoffees.Add(ingredientCoffees);
context.SaveChanges();
}
它将创建您的新行以及关联表中的正确ID。