Skip to content

Instantly share code, notes, and snippets.

@cmatskas
Created May 12, 2015 11:15
Show Gist options
  • Save cmatskas/e1840617f07a7ba77096 to your computer and use it in GitHub Desktop.
Save cmatskas/e1840617f07a7ba77096 to your computer and use it in GitHub Desktop.
public virtual void Update(TEntity entityToUpdate)
{
var entry = this.context.Entry(entityToUpdate);
var key = this.GetPrimaryKey(entry);
if (entry.State == EntityState.Detached)
{
var currentEntry = this.databaseSet.Find(key);
if (currentEntry != null)
{
var attachedEntry = this.context.Entry(currentEntry);
attachedEntry.CurrentValues.SetValues(entityToUpdate);
}
else
{
this.databaseSet.Attach(entityToUpdate);
entry.State = EntityState.Modified;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment