1

I have 6 related tables. I am using a view model to show the properties from all 6 tables in my view. I am able to add data in single transaction using the above structure. But while editing the data I get - "Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded." error message.

From the message it seems that the 0 rows were affected in some table. In my view it might be possible that not every value will be edited. Only some values belonging to some table will be edited and some will be stored as it is. So if the values from one table are not at all edited and if I try to do the following, then the above error pops up:

db.Entry(tbl).State = EntityState.Modified;
db.SaveChanges();

Is there a way to Modify the entity state of only those tables whose values are edited in the Edit View? Or is there any other better approach for this?

halfer
  • 19,824
  • 17
  • 99
  • 186
arpymastro
  • 751
  • 3
  • 16
  • 34
  • http://stackoverflow.com/questions/1836173/entity-framework-store-update-insert-or-delete-statement-affected-an-unexpec – Nsevens Sep 21 '16 at 10:53
  • @Nsevens Thanks for the url but I have already went through it and that did not helped to resolve my error. – arpymastro Sep 21 '16 at 11:01

2 Answers2

2

For a project here we did the following:

Perhaps an important one, is the Context.People.Attach() method.

Context.People.Attach(person);

// Disable validation otherwise you can't do a partial update
Context.Configuration.ValidateOnSaveEnabled = false;

Context.Entry(person).Property(x => x.AddressId).IsModified = true;
Context.Entry(person).Property(x => x.Birthday).IsModified = true;
Context.Entry(person).Property(x => x.Name).IsModified = true;
...

await Context.SaveChangesAsync();

Perhaps this is something you can work with? Not sure if the same approach can help for your case.

Nsevens
  • 2,588
  • 1
  • 17
  • 34
2

Example of editing an entity:

//Get the database entry
var entity = db.Person.First(c=>c.ID == 1);
//OR
//Attach a object to the context, see Nsevens answer.
db.Person.Attach(entity);

//Change a property
entity.Job = "Accountant";

//State it's a modified entry
db.Entry(entity).State = EntityState.Modified;

//Save
db.SaveChanges();

If you are editing multiple entries you will need to set every one to EntityState.Modifed, for example in a foreach loop.

Wurd
  • 465
  • 2
  • 15
  • In the above example the single property(Job) of the entity is modified. Will this also works if there is no modification done on the property? Or is there a way to track if there is new/updated value provided for the property(from the view) then only the entity state is modified? – arpymastro Sep 22 '16 at 05:14
  • That sounds like logic that you will have to build in your app. Either manually track if any entity has been changed or compare every entity to your database objects. Ideally you shouldn't change `EntitiyState` on items that haven't been modified, but if the number of edited entities are relatively small it could be ok, as it would waste resources. – Wurd Sep 22 '16 at 08:01