title: The right way to insert multiple records to a table using LINQ to Entities
body: <p>As many of us have done, I set up a simple loop to add multiple records from a databse. A prototypical example would be something like this:</p>
  <h2>Method I:</h2>
  <pre><code>// A list of product prices
  List&lt;int&gt; prices = new List&lt;int&gt; { 1, 2, 3 };
  
  NorthwindEntities NWEntities = new NorthwindEntities();
  
  foreach (int price in prices)
  {
     Product newProduct = new Product();
     newProduct.Price = price;
     NWEntities.Products.AddObject(newProduct);
  }
  
  NWEntities.SaveChanges();
  </code></pre>
  <p>When I first set up the loop, however, I intuitively wrote:</p>
  <h2>Method II:</h2>
  <pre><code>Product newProduct = new Product();
     
  foreach (int price in prices)
  {
     newProduct.Price = price;
     NWEntities.Products.Add(newProduct);
  }
  </code></pre>
  <p>After doing a little reading, several individuals mentioned that if the <strong>Method II</strong> is used only one record would be added to the table. This seems counter intuitive. It's the Add() function which load a new insertion, and, I would think, creates an object after each call with the data passed in. Declaring my Product object <em>outside</em> the loop would seem to better utilize resources, as the only overhead consumed in each call would be the re-assignment of the object instance property, and not the re-construction of the object instance itself.</p>
  <p>Can anyone please clarify? I could not find another post that deals with this question directly. If one is out there please point to it.</p>