title: SQLBulkCopy inserts using Entity Framework with Foreign Key object
body: <p>I am using EF6 as the Load layer of an ETL tool. I acknowledge there are better tools (such as SSIS, direct SQL queries, etc) however due to the complexity of the transformation, it needed to be done in code, and the target DB is built from an EF Model. The number of records inserted in a batch can <em>exceed 100,000</em> records. This is not incredibly slow to do (using the <code>DBContext.AddRange()</code> method) but the memory usage is extremely high (exceeding <code>1GB</code>)</p>
  
  <p>For examples sake, i have the following data classes (which are created in memory)</p>
  
  <pre><code>    public class Foo
      {
          public long FooID { get; set; }
          public string SomeProperty { get; set; }
          public decimal AverageFlightSpeedOfUnladenSwallow { get; set; }
          public IEnumerable&lt;Bar&gt; Bars { get; set; }
      }
  
      public class Bar
      {
          public long BarID { get; set; }
          public Foo Foo { get; set; }
          public long FooID { get; set; }
          public string FavoriteColour { get; set; }
      }
  dbContext.Foos.AddRange(ListOfFoos); //Pre constructed list of Foos
  dbContext.Bars.AddRange(ListOfBars); //Pre constructed list of Bars (parent Foo items populated, FooID is not)
  dbContext.SaveChanges();
  </code></pre>
  
  <p>I am looking at using the LINQ Entity Data reader to enable the conversion of <code>IList&lt;Foo&gt;</code> to a data reader so i can import it using SQLBulkCopy (<a href="https://stackoverflow.com/questions/2553545/sqlbulkcopy-and-entity-framework">SqlBulkCopy and Entity Framework</a>, 
  <a href="http://archive.msdn.microsoft.com/LinqEntityDataReader/Release/ProjectReleases.aspx?ReleaseId=389" rel="nofollow noreferrer">http://archive.msdn.microsoft.com/LinqEntityDataReader/Release/ProjectReleases.aspx?ReleaseId=389</a>).</p>
  
  <p><strong>Requirement</strong> <br>
  The <code>List&lt;Bar&gt;</code> will not have the ID's of the parent <code>Foo</code> class. Entity framework handles this just fine, but i am not sure how to get this same functionality in SqlBulkCopy. Is there some way to get it done?</p>