Reputation: 58464
On my ASP.NET MVC application, I am trying update two related models as follows:
if (ModelState.IsValid) {
_accommpropertyseasonrepo.Edit(accommPropertySeasonCreateViewModel.AccommPropertySeason);
_accommpropertyseasonrepo.Save();
_accommpropertyseasondetailrepo.Edit(accommPropertySeasonCreateViewModel.AccommPropertySeasonDetail);
_accommpropertyseasondetailrepo.Save();
return RedirectToAction("Details", new { id = id });
}
First one is passing through with no problem but when it tries to update the second one, concurrency is kinking in and throws the below 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
what is the way of achieving what I am trying to do here?
UPDATE
Here is the _accommpropertyseasondetailrepo
Edit
method:
public void Edit(AccommPropertySeasonDetail entity) {
_context.Entry(entity).State = System.Data.EntityState.Modified;
}
Here is the _accommpropertyseasonrepo
Edit
method:
public void Edit(AccommPropertySeason entity) {
_context.Entry(entity).State = System.Data.EntityState.Modified;
}
UPDATE 2
Here is the entire action method:
public ActionResult SeasonEdit_post(int id, int subid,
[Bind(Exclude =
"AccommPropertySeason.CreatedBy,AccommPropertySeason.CreatedOn")]
AccommPropertySeasonCreateViewModel accommPropertySeasonCreateViewModel) {
if (ModelState.IsValid) {
_accommpropertyseasonrepo.Edit(accommPropertySeasonCreateViewModel.AccommPropertySeason);
_accommpropertyseasonrepo.Save();
_accommpropertyseasondetailrepo.Edit(accommPropertySeasonCreateViewModel.AccommPropertySeasonDetail);
_accommpropertyseasondetailrepo.Save();
return RedirectToAction("Details", new { id = id });
}
return View(accommPropertySeasonCreateViewModel);
}
UPDATE
Brutal Truth: I forgot the following line of code on my view so the model was coming back without PK :
@Html.HiddenFor(m => m.AccommPropertySeasonDetail.AccommPropertySeasonDetailID)
Problem solved now!
Upvotes: 0
Views: 338
Reputation: 156564
Copy/pasted from my comment on the OP
This doesn't look like it has anything to do with concurrency. For some reason, the way the SQL Update statements are being written, Entity Framework is expecting items to be updated and those items aren't there. Is there any chance you can attempt to run the same code in LINQPad and watch the SQL results to see what's happening?
Upvotes: 1
Reputation: 33071
There might be a perfectly good reason you are doing this, but without knowing that reason this looks like a very bad way of accomplishing what you are doing.
You should be querying the database to make sure the entity exists, and then if it does you should update the entity and save those changes.
var entity = repository.GetAccomPropertySeason(id);
if( entity == null )
return View("NotFound");
// update the model here
repository.SaveChanges();
The way you have this written, if the object doesn't actually exist, then you are going to get this error.
Are you sure your Details object has the correct parent id? Is it getting bound properly? From the looks of it you are telling the context that it exists and you are going to update it, but the context can't actually find the entity when it calls SaveChanges.
If the intent was to create a new object (not modify an existing one) then you should set the state to Added instead.
Upvotes: 2