title: Using savechanges() inside foreach loop c#
body: <p>I have this method below that is writing data to two tables in the database. There is a collection I need to write to the database in the foreach section. why saveChanges does not work in each iteration of the loop and is there a better way of doing this?
  there is two things... first:the function does not return any thing .... second:the database did not up to date .... when i removed savechange() from the function it has works but with out changing values in database .</p>
  
  <pre><code>if (_context.ProductPins.Where(a =&gt; a.ProductId == id &amp;&amp; a.Status==false).Count() &gt; 0)
          {
              var c = 0;
              var ProductPins = _context.ProductPins.Where(a=&gt;a.Status==false &amp;&amp; a.ProductId == id);
              foreach(var item in ProductPins)
              {
                  ApplicationUser.Balance = ApplicationUser.Balance - product.BuyingPrice;
                  item.Equals(true);
                  _context.Statements.Add(new Statement
                  {
                      Amount = product.BuyingPrice,
                      RecordDate = DateTime.Now,
                      Destination = false,
                      FromApplicationUserId = _userManager.GetUserId(User),
                      //ToApplicationUserId = nu,
                      BalanceType = BalanceType.currentbalance,
                  });
  
                  _context.Update(ApplicationUser);
                  _context.Update(item);
                  _context.SaveChanges();
                  c++;
                  if (c &gt; count)
                  {
                      break;
                  }
  
  
  
  
  
              }
  </code></pre>