title: How to seed data with many-to-may relations in Entity Framework Migrations
body: <p>I use entity framework migration (in Automatic migration mode). Everything is okay, but I have one question:</p>
  
  <p>How should I seed data when I have many-to-many relationships?</p>
  
  <p>For example, I have two model classes:</p>
  
  <pre><code>public class Parcel
  {
      public int Id { get; set; }
      public string Description { get; set; }
      public double Weight { get; set; }
      public virtual ICollection&lt;BuyingItem&gt; Items { get; set; }
  }
  
  public class BuyingItem
  {
      public int Id { get; set; }
      public decimal Price { get; set; }
      public virtual ICollection&lt;Parcel&gt; Parcels { get; set; }
  }
  </code></pre>
  
  <p>I understand how to seed simple data (for PaymentSystem class) and one-to-many relationships, but what code should I write in the <code>Seed</code> method to generate some instances of <code>Parcel</code> and <code>BuyingItem</code>? I mean using <code>DbContext.AddOrUpdate()</code>, because I don't want to duplicate data every time I run <code>Update-Database</code>.</p>
  
  <pre><code>protected override void Seed(ParcelDbContext context)
  {
      context.AddOrUpdate(ps =&gt; ps.Id,
          new PaymentSystem { Id = 1, Name = "Visa" },
          new PaymentSystem { Id = 2, Name = "PayPal" },
          new PaymentSystem { Id = 3, Name = "Cash" });
  }
  </code></pre>
  
  <hr>
  
  <pre><code>protected override void Seed(Context context)
  {
      base.Seed(context);
  
      // This will create Parcel, BuyingItems and relations only once
      context.AddOrUpdate(new Parcel() 
      { 
          Id = 1, 
          Description = "Test", 
          Items = new List&lt;BuyingItem&gt;
          {
              new BuyingItem() { Id = 1, Price = 10M },
              new BuyingItem() { Id = 2, Price = 20M }
          }
      });
  
      context.SaveChanges();
  }
  </code></pre>
  
  <p>This code creates <code>Parcel</code>, <code>BuyingItems</code> and their relationship, but if I need the same <code>BuyingItem</code> in another <code>Parcel</code> (they have a many-to-many relationship) and I repeat this code for the second parcel - it will duplicate <code>BuyingItems</code> in the database (though I set the same <code>Id</code>s). </p>
  
  <p>Example:</p>
  
  <pre><code>protected override void Seed(Context context)
  {
      base.Seed(context);
  
      context.AddOrUpdate(new Parcel() 
      { 
          Id = 1, 
          Description = "Test", 
          Items = new List&lt;BuyingItem&gt;
          {
              new BuyingItem() { Id = 1, Price = 10M },
              new BuyingItem() { Id = 2, Price = 20M }
          }
      });
  
      context.AddOrUpdate(new Parcel() 
      { 
          Id = 2, 
          Description = "Test2", 
          Items = new List&lt;BuyingItem&gt;
          {
              new BuyingItem() { Id = 1, Price = 10M },
              new BuyingItem() { Id = 2, Price = 20M }
          }
      });
  
      context.SaveChanges();
  }
  </code></pre>
  
  <p>How can I add the same <code>BuyingItem</code> in different <code>Parcel</code>s?</p>