title: Seeding items with relations
body: <p>Consider the following:</p>
  
  <pre><code>public class Foo
  {
      [Key]
      public int Id { get; set; }
  
      public string Name { get; set; }
  
      public string Something { get; set; }
  
      public virtual ICollection&lt;Bar&gt; Bars { get; set; }
  }
  
  public class Bar
  {
      [Key]
      public int Id { get; set; }
  
      public string Name { get; set; }
  
      public string Something { get; set; }
  
      [ForeignKey("Foo")]
      public int FooId { get; set; }
      public virtual Foo Foo { get; set; }
  }
  </code></pre>
  
  <p>Now, in my context, I want to seed some <code>Foo</code>s with related <code>Bar</code>s:</p>
  
  <pre><code>context.Foos.AddOrUpdate(
      r =&gt; r.Name,
      new Foo
      {
          Name = "Foo 1",
          Something = "Blah",
          Bars = new List&lt;Bar&gt;
          {
              new Bar { Name = "Foo 1 Bar 1", Something = "Blah" },
              new Bar { Name = "Foo 1 Bar 2", Something = "Blah" },
              new Bar { Name = "Foo 1 Bar 3", Something = "Blah" },
          }
      },
      ...
  </code></pre>
  
  <p>If I run <code>Update-Database</code> everything works as you'd expect, and I get the related <code>Bar</code>s added for each <code>Foo</code>.</p>
  
  <p>Now, if I go and change my seed data to something like:</p>
  
  <pre><code>context.Foos.AddOrUpdate(
      r =&gt; r.Name,
      new Foo
      {
          Name = "Foo 1",
          Something = "BlahBlahBlah",
          Bars = new List&lt;Bar&gt;
          {
              new Bar { Name = "Foo 1 Bar 1", Something = "BlahBlahBlah" },
              new Bar { Name = "Foo 1 Bar 2", Something = "BlahBlahBlah" },
              new Bar { Name = "Foo 1 Bar 3", Something = "BlahBlahBlah" },
          }
      },
      ...
  </code></pre>
  
  <p>And run <code>Update-Database</code> again, the <code>Foo</code>s are updated, but all of the <code>Bar</code>s will still have "Blah" for their <code>Something</code> properties. This makes sense, since there's no logical way for Entity Framework to know which <code>Bar</code> should be updated, but is there any workaround? Perhaps some other way I can add the <code>Bar</code>s and also keep them related to the <code>Foo</code>s? I know I could simply do:</p>
  
  <pre><code>context.Bars.AddOrUpdate(
      r = r.Name,
      new Bar
      {
          ...
      },
      ...
  </code></pre>
  
  <p>But given that I'm using identity columns as keys, I have no way to know which <code>Bar</code>s to add to which <code>Foo</code>s. Any ideas?</p>