I am coding a MVC5 internet application and am using EF6.
I have an Edit
ActionResult
that is called when an Asset
object is edited. I also need to update other objects values when an Asset
object is edited. The UpdateAssociatedAssetObjects
function does this.
I am getting the following error:
There is already an open DataReader associated with this Command which must be closed first.
In the UpdateAssociatedAssetObjects
function, at the following line of code:
if (item.mapMarker.Id == asset.Id)
Here is the Edit ActionResult
:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(AssetViewModel assetViewModel)
{
if (ModelState.IsValid)
{
db.Entry(assetViewModel.asset).State = EntityState.Modified;
assetViewModel.asset.lastUpdate = DateTime.Now;
if (assetViewModel.asset.linkFromExternalResource)
{
assetViewModel.asset.webAddress = assetViewModel.webAddress;
}
else
{
assetViewModel.asset.webAddress = assetViewModel.filename;
}
db.Entry(assetViewModel.asset).Property(uco => uco.creationDate).IsModified = false;
db.Entry(assetViewModel.asset).Property(uco => uco.userName).IsModified = false;
assetService.UpdateAssociatedAssetObjects(db, assetViewModel.asset);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(assetViewModel);
}
Here is the UpdateAssociatedAssetObjects
function:
public void UpdateAssociatedAssetObjects(CanFindLocationDatabaseContext db, Asset asset)
{
foreach (var item in db.mapLocations)
{
if (item.mapMarker.Id == asset.Id)
{
item.lastUpdate = DateTime.Now;
}
}
}
Can I please have some help with this code?
I have tried placing the UpdateAssociatedAssetObjects
function after the await db.SaveChangesAsync()
and using a new database context object, but the error still occurs.
Thanks in advance