I'm trying to build a many to many relationship using the entity framework and fluent API, but I'm stuck trying to allow duplicate entries. Here is what I have:
我正在尝试使用实体框架和fluent API来构建很多到很多的关系,但是我仍然试图允许重复的条目。以下是我的资料:
public class Pizza
{
public int PizzaId { get; set; }
public virtual ICollection<Topping> Toppings { get; set; }
}
public class Topping
{
public int ToppingId { get; set; }
}
- Any pizza should be able to have multiple toppings.
- 任何披萨都应该有多种配料。
- Any topping can be applied to multiple pizzas.
- 任何浇头都可以应用到多个披萨上。
So in OnModelCreating()
I call:
在onmodelcreation()中,我调用:
modelBuilder.Entity<Pizza>()
.HasMany(p => p.Toppings)
.WithMany()
.Map(m => m.ToTable("ToppingsForPizza"));
That give me a nice many-to-many relationship, but the problem is I want a pizza to be able to have multiple instances of the same topping e.g. double pepperoni
这给了我一个很好的多对多关系,但问题是我想要一个披萨能够有多个相同的例子,例如双辣香肠
The ToppingsForPizza
database that is generated cannot support that... i'm guessing because there needs to be a unique primary key.
生成的ToppingsForPizza数据库不能支持……我猜是因为需要有一个唯一的主键。
Is there a way to do this?
有办法吗?
EDIT: My actual problem has nothing to do with pizza, this is just the example I came up with.
编辑:我的实际问题与披萨无关,这只是我想到的一个例子。
3 个解决方案
#1
2
You need to add a different primary key to your many to many relationship. That means it becomes an entity in it's own right
你需要在许多关系中添加一个不同的主键。这意味着它本身就成为一个实体
public class PizzaTopping
{
public int PizzaToppingId { get; set; }
public int PizzaId { get; set; }
public int ToppingId { get; set; }
public virtual Pizza Pizza { get; set; }
public virtual Topping Topping { get; set; }
}
public class Pizza
{
public int PizzaId { get; set; }
public virtual ICollection<PizzaTopping> PizzaToppings { get; set; }
}
public class Topping
{
public int ToppingId { get; set; }
public virtual ICollection<PizzaTopping> PizzaToppings { get; set; }
}
Doesn't make much sense applied to Pizzas and Toppings..... ;-)
把它应用到披萨和配料上没有多大意义……:-)
#2
0
I think you should consider double pepperoni as a separate Topping. But, you can also create a third class, like below:
我认为你应该考虑把双份意大利香肠作为单独的配料。但是,您也可以创建第三类,如下所示:
public class PizzaTopping
{
public int PizzaId { get; set; }
public int ToppingId { get; set; }
public int ToppingCount { get; set; }
public virtual ICollection<Pizza> Pizzas { get; set; }
public virtual ICollection<Topping> Toppings { get; set; }
}
public class Pizza
{
public int PizzaId { get; set; }
}
public class Topping
{
public int ToppingId { get; set; }
}
#3
0
Another possible solution is to turn off code first and model verification, create a new primary key as described by others, create foreign keys in your relationship table for PizzaId and ToppingId, then modify your mapping to include the foreign keys.
另一种可能的解决方案是首先关闭代码和模型验证,创建别人描述的新主键,在关系表中为PizzaId和ToppingId创建外键,然后修改映射以包含外键。
modelBuilder.Entity<Pizza>()
.HasMany(p => p.Toppings)
.WithMany()
.Map(m => m.ToTable("ToppingsForPizza")
.MapLeftKey("PizzaId")
.MapRightKey("ToppingId"));
(Because it's likely that it's a unique composite key that's preventing the addition of multiple toppings.)
(因为它可能是一个独特的复合键,可以防止添加多个浇头。)
#1
2
You need to add a different primary key to your many to many relationship. That means it becomes an entity in it's own right
你需要在许多关系中添加一个不同的主键。这意味着它本身就成为一个实体
public class PizzaTopping
{
public int PizzaToppingId { get; set; }
public int PizzaId { get; set; }
public int ToppingId { get; set; }
public virtual Pizza Pizza { get; set; }
public virtual Topping Topping { get; set; }
}
public class Pizza
{
public int PizzaId { get; set; }
public virtual ICollection<PizzaTopping> PizzaToppings { get; set; }
}
public class Topping
{
public int ToppingId { get; set; }
public virtual ICollection<PizzaTopping> PizzaToppings { get; set; }
}
Doesn't make much sense applied to Pizzas and Toppings..... ;-)
把它应用到披萨和配料上没有多大意义……:-)
#2
0
I think you should consider double pepperoni as a separate Topping. But, you can also create a third class, like below:
我认为你应该考虑把双份意大利香肠作为单独的配料。但是,您也可以创建第三类,如下所示:
public class PizzaTopping
{
public int PizzaId { get; set; }
public int ToppingId { get; set; }
public int ToppingCount { get; set; }
public virtual ICollection<Pizza> Pizzas { get; set; }
public virtual ICollection<Topping> Toppings { get; set; }
}
public class Pizza
{
public int PizzaId { get; set; }
}
public class Topping
{
public int ToppingId { get; set; }
}
#3
0
Another possible solution is to turn off code first and model verification, create a new primary key as described by others, create foreign keys in your relationship table for PizzaId and ToppingId, then modify your mapping to include the foreign keys.
另一种可能的解决方案是首先关闭代码和模型验证,创建别人描述的新主键,在关系表中为PizzaId和ToppingId创建外键,然后修改映射以包含外键。
modelBuilder.Entity<Pizza>()
.HasMany(p => p.Toppings)
.WithMany()
.Map(m => m.ToTable("ToppingsForPizza")
.MapLeftKey("PizzaId")
.MapRightKey("ToppingId"));
(Because it's likely that it's a unique composite key that's preventing the addition of multiple toppings.)
(因为它可能是一个独特的复合键,可以防止添加多个浇头。)