1

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;
                }





            }
9
  • Are you using Entity Framework? I know Dapper allows you to actually do collection based inserts, but that looks like Entity Framework is that correct? Commented Oct 8, 2019 at 22:10
  • OP, please specify what you mean by "does not work." Do you get an error? Exception? No error but data is not saved? Data is saved but not correctly? Or does it work to spec but you just don't like the code structure? Commented Oct 8, 2019 at 22:45
  • @Greg yes i am using entity framework Commented Oct 8, 2019 at 23:15
  • @JohnWu 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 Commented Oct 8, 2019 at 23:19
  • @محمدالعاني - if you're running sql server as backend, could you check using sql profiler? SaveChanges() should return int showing number of records saved. (on side note, you can change _context.ProductPins.Where(a => a.ProductId == id && a.Status==false).Count() > 0 to _context.ProductPins.Any(a => a.ProductId == id && a.Status==false) and put _userManager.GetUserId(User) outside of the loop). Commented Oct 9, 2019 at 1:01

2 Answers 2

2

If you are saving to a database, you should queue these changes so that you can do one write at the end and process all of them at once rather than one at a time, this way you take advantage of the databases internal write processing.

Sign up to request clarification or add additional context in comments.

Comments

0

I only put savechange() method out of froreach loop....savechanges() should be not inside loop

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);

                c++;
                if (c > count)
                {
                    break;
                }





            }
_context.SaveChanges();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.