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 .
if (_context.ProductPins.Where(a => a.ProductId == id && a.Status==false).Count() > 0)
{
var c = 0;
var ProductPins = _context.ProductPins.Where(a=>a.Status==false && 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 > count)
{
break;
}
}