I am trying to update a database entry using entity framework. The entities are as follows:
public partial class Test
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid identity { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public TestRef Colour { get; set; }
}
public class TestRef
{
public int id { get; set; }
public string favColor { get; set; }
}
and the edit ActionResult in the relevant Controller is as follows:
public ActionResult Edit([Bind(Include = "identity,Name,Age,Colour")] Test test)
{
if (ModelState.IsValid)
{
test.Colour = db.TestRefs.Find(test.Colour.id);
db.Tests.Attach(test);
db.Entry(test).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(test);
}
So here, Edit seems to work for all the properties in Test except for Colour (in that all the other properties get updated, but Colour remains as it was before, with no change). I am assuming this is because it is an association, but I can't for the life of me figure out why.