title: Batch update on object list using EntityFramework 6 and Linq
body: <p>I want to create a function that accepts a list of objects with their properties already set and does a batch update or insert on them only making 1 call to the database.</p>
  
  <p><strong>(UPDATE) Here is the working version:</strong></p>
  
  <pre><code>    public static void BatchInsertProducts(IList&lt;Product&gt; productList)
      {
          using (var context = new DbContext())
          {
              context.Products.AddRange(productList);
              context.SaveChanges();
          }
      }
  </code></pre>
  
  <p>This is my attempt at doing either and update or an insert on several items based on the Id. I don't think I can use AddRange here so I tried it this way and I am getting an error:</p>
  
  <pre><code>    public static void SaveMultipleProducts(IList&lt;Product&gt; productList)
      {
          using (var context = new DbContext())
          {
              foreach (Account p in productList)
              {
                  if (p.ID == 0)
                  {
                      context.Entry(p).State = EntityState.Added;
                      p.InsertUserId = "jtunney";
                      p.InsertDate = DateTime.Now;
                  }
                  else
                  {
                      context.Entry(p).State = EntityState.Modified;
                      p.UpdateUserId = "jtunney";
                      p.UpdateDate = DateTime.Now;
                  }
              }
              context.SaveChanges();
          }
      }
  </code></pre>