title: Entity Framework - Inserting a Parent and Child model with FK simultaneously
body: <p>I have an ASP.NET MVC application using Entity Framework. I have developed 2 models, using the database first (code) approach for a SQL Server 2008 R2 database. One model is essentially a parent model, and the other is a child model that contains a foreign key to a database generated column of the parent model.</p>
  
  <p>Pseudo code:</p>
  
  <pre><code>CREATE TABLE PARENT
  (
      ROWID BIGINT IDENTITY(1,1) NOT NULL,
      VALUE NVARCHAR(255) NOT NULL,
  
      PRIMARY KEY(ROWID)
  );
  
  CREATE TABLE CHILD
  (
      ROWID BIGINT IDENTITY(1,1) NOT NULL,
      PARENT_ROWID BIGINT NOT NULL,
      VALUE NVARCHAR(255) NOT NULL,
  
      PRIMARY KEY(ROWID),
      FOREIGN KEY(PARENT_ROWID) REFERENCES PARENT(ROWID)
  );
  </code></pre>
  
  <p>All of this works fine, and I'm able to bind my data to my model and edit the fields and so on. My question is, if I am trying to create records for these models, how can I do that when the foreign key for CHILD is in PARENT which isn't yet available if I create them at the same time?</p>
  
  <p>So, my current basic steps are:</p>
  
  <ol>
  <li>Get a list of the <code>PARENT</code> models to insert into the database </li>
  <li>Insert the <code>PARENT</code> models </li>
  <li><p>Call <code>SaveChanges()</code></p>
  
  <ul>
  <li><p>For each PARENT:</p>
  
  <ol>
  <li>Get FK for PARENT</li>
  <li>Get a List of the CHILD models to insert into the database</li>
  <li>For each CHILD model, insert CHILD with that FK</li>
  <li>Call <code>SaveChanges()</code></li>
  </ol></li>
  </ul></li>
  <li><p>Additional operations and updates to database for other models </p></li>
  <li>Call <code>SaveChanges()</code></li>
  </ol>
  
  <p>I'm sure I could optimize that, but that's not what I'm concerned with. It seems to me like there should be some way to do this without manually looking up the FK or manually setting the FK. If I have to, I can do this, but I would like to use only one <code>SaveChanges()</code> call to push these commands through in a single transaction. I'm looking for something along the lines of:</p>
  
  <ol>
  <li><p>Get a List of the PARENT models to insert into the database </p>
  
  <ul>
  <li>For each PARENT
  
  <ol>
  <li>Insert PARENT and return new FK (without saving)</li>
  <li>Get a List of the CHILD models to insert into the database</li>
  <li>For each CHILD model, insert CHILD with that FK</li>
  </ol></li>
  </ul></li>
  <li><p>Additional operations and updates to Database for other models </p></li>
  <li>Call <code>SaveChanges()</code></li>
  </ol>
  
  <p>To provide some context, my actual table has many columns that aren't guaranteed to be unique (which is why the PK is on ROWID), and each of those rows has a none to many relationships with the child table, where the child table also may have non-unique rows. Not very ideal, but it's what our operations require as of now. I included pseudo-code here because the actual code is proprietary, but I could probably create a real mock-up if needed.</p>
  
  <p>Any suggestions?</p>
  
  <p>Thank you</p>
  
  <p><strong>EDIT:</strong>
  So I changed my code to make use of the navigation property as suggested. This almost works. Here is my code:</p>
  
  <pre><code>List&lt;PARENT&gt; parents = getParents();
  List&lt;CHILD&gt; children = getChildren();
  foreach (PARENT parent in parents)
  {
      db.PARENT.add(parent);
      foreach (CHILD child in children)
      {
          child.PARENT = parent;
          db.CHILD.add(child);
      }
  }
  
  db.SaveChanges();
  </code></pre>
  
  <p>At first glance, this is exactly what I was looking for. However, when I run it, the child records are only being inserted for the first parent record. So, for example, if I have 3 PARENT records, and 3 CHILD records, I should have 9 new records in my CHILD table. However, after this code, I only have 3 records for the first PARENT. </p>
  
  <p>I'm assuming this has something to do with the fact that I'm using the same list of children repeatedly and Entity Framework is viewing them as duplicates somehow. I moved it into the PARENT for loop as follows and it works as expected:</p>
  
  <pre><code>List&lt;PARENT&gt; parents = getParents();
  foreach (PARENT parent in parents)
  {
      db.PARENT.add(parent);
      List&lt;CHILD&gt; children = getChildren();
      foreach (CHILD child in children)
      {
          child.PARENT = parent;
          db.CHILD.add(child);
      }
  }
  
  db.SaveChanges();
  </code></pre>
  
  <p>But, I would prefer not to do this as the getChildren() function is rather cumbersome. Any thoughts?</p>