Many-to-many relationship with duplicate entries

拈花ヽ惹草 提交于 2019-12-02 01:49:16

问题


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:

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:

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.

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.


回答1:


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:


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:


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.

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.)



来源:https://stackoverflow.com/questions/19171514/many-to-many-relationship-with-duplicate-entries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!