I have an Edit controller method that takes a Tool
from an Edit view.
The Tool model contains a MobileUser
navigation property with name Holder
, and it also contains a string HolderName
, that's used to set a holder in the view.
In the controller method I set the MobileUser
by getting the user with Tool.HolderName == MobileUser.Name
, but after db.SaveChanges()
is called the MobileUser
navigation property is not changes in the database.
My code:
ToolsController.Edit(Tool tool):
int id = db.MobileUsers.Single(x => x.Name == tool.HolderName).Id;
tool.Holder = db.MobileUsers.Find(id);
if (ModelState.IsValid)
{
db.Entry(tool).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(tool);
The Holder navigation property in Tool class:
public virtual MobileUser Holder { get; set; }
I eventually gave up and used GraphDiff instead. Works very well and I recommend it if anyone runs into this problem in the future.