This code 开发者_Python百科is giving an error when trying to update the database
Error:An object with a key that matches the key of the supplied object could not be found in the ObjectStateManager. Verify that the key values of the supplied object match the key values of the object to which changes must be applied.
public void UpdateAccuralSettings(SystemTolerance updatedObject)
{
_source.SystemTolerances.ApplyCurrentValues(updatedObject);
_source.SaveChanges();
}
ApplyCurrentValues
works only if entity is first loaded from the database (if you don't use the same context you used for loading it before it is most probably not):
public void UpdateAccuralSettings(SystemTolerance updatedObject)
{
_source.SystemTolerances.Single(x => x.Id == updatedObject.Id);
_source.SystemTolerances.ApplyCurrentValues(updatedObject);
_source.SaveChanges();
}
If you want to just save your current data without reloading entity use:
public void UpdateAccuralSettings(SystemTolerance updatedObject)
{
_source.SystemTolerances.Attach(updatedObject);
_source.ObjectStateManager.ChangeEntityState(updatedObject, EntityState.Modified);
_source.SaveChanges();
}
could be that you model.edmx is not up-to-date ? // database has changed ?
depending on you setup/environment i think just a .SaveChanges() on the right Entitie-Context ( from which updatedObject has been created ) would do the update in db.
so ev just _source.SaveChanges();
greetings
精彩评论