title: How to Update/Edit multiple records using Entity Framework in ASP.NET MVC
body: <p>I need to update multiple records, this is how far I could get</p>
  
  <p>First, I tried this</p>
  
  <pre><code>foreach (var Product in Products)
  {
       Product.Price = Price;
  
       db.Entry(Product).State = EntityState.Modified;
       db.SaveChanges();
   }
  </code></pre>
  
  <p>But, I'm getting this error</p>
  
  <blockquote>
    <p>Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.</p>
  </blockquote>
  
  <p>Then, I found <a href="https://stackoverflow.com/questions/8254854/object-with-same-key-already-exists-in-objectstatemanager">this</a> answer, saying that I should create new instance for each iteration, but I got the same error. Below is my attempt.</p>
  
  <pre><code>foreach (var Product in Products)
  {
       var ProductInLoop = new Product();
       ProductInLoop.Price = Product.Price;
  
       db.Entry(ProductInLoop).State = EntityState.Modified;
       db.SaveChanges();
   }
  </code></pre>
  
  <p>Also I can't do it like this <code>db.Entry(Products).State = EntityState.Modified;</code> outside the loop, because <code>Entry()</code> expects a singel object.</p>
  
  <p>How can I solve this?</p>