1

I need to update multiple records, this is how far I could get

First, I tried this

foreach (var Product in Products)
{
     Product.Price = Price;

     db.Entry(Product).State = EntityState.Modified;
     db.SaveChanges();
 }

But, I'm getting this error

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.

Then, I found this answer, saying that I should create new instance for each iteration, but I got the same error. Below is my attempt.

foreach (var Product in Products)
{
     var ProductInLoop = new Product();
     ProductInLoop.Price = Product.Price;

     db.Entry(ProductInLoop).State = EntityState.Modified;
     db.SaveChanges();
 }

Also I can't do it like this db.Entry(Products).State = EntityState.Modified; outside the loop, because Entry() expects a singel object.

How can I solve this?

5
  • Why are you looping through Products yet repeatedly setting the values of ProductSize or ProductInLoop and ignoring the loop variable Product? Commented Sep 10, 2015 at 12:16
  • Why are u creating new Productsin your loop? You want to update the existing ones, right? Commented Sep 10, 2015 at 12:21
  • Thanks @BenRobinson , the problem was like you said, it was the Primary Key = 0, I solved it thanks to you. Regarding ProductSize, I'm actually updating ProductSize, instead of Product, but I removed all unrelated code trying to simplify/focus on the problem, but I forgot to change it. Commented Sep 10, 2015 at 12:21
  • 1
    please call savechanges outside the loop call it only one time Commented Sep 10, 2015 at 12:49
  • @BenRobinson can you sir see my post same error how to solve it and I dont know I passwed the primary key in correct way or not ? stackoverflow.com/questions/72825364/… Commented Jul 1, 2022 at 7:30

2 Answers 2

3

Thanks to Ben Robinson's comment, I solved this. The problem caused because I didn't pass the primary key from View to Controller.

In View I added this hidden field to pass the primary keys like this:

<input type="hidden" name="[@i].id" value="@Product.id" />

and server side code look like this

foreach (var product in Products)
{
    product.Price = Price;

    db.Entry(product).State = EntityState.Modified;
 }
 db.SaveChanges();
Sign up to request clarification or add additional context in comments.

1 Comment

Hello sir can you please see the error same this error but I don't know where is the error stackoverflow.com/questions/72825364/…
1

There is no need to create a new Product object. Since you are modifying records, your Products collection is likely already attached to the database context, and it's state will be tracked if you modify it.

Try this:

foreach (var Product in Products)
{        
     Product.price = ProductSize.price;
     db.SaveChanges();
}

if the Products are not attached to the context (db), add this line before saving:

db.Products.Attach(Product); 

by convention, you should also change your looping variable from Prodcut to the lowercase form : product

1 Comment

I solved it thanks to Ben Robinson comment. I didn't pass the primary key from view to controller, and it was equal to zero, and that caused the error. Once I passed it, the problem resolved. Also thanks for the convention advice.

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.