Skip to content

Instantly share code, notes, and snippets.

@Zonciu
Created January 8, 2019 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zonciu/6569212b87a01f4c495feee2baed1f17 to your computer and use it in GitHub Desktop.
Save Zonciu/6569212b87a01f4c495feee2baed1f17 to your computer and use it in GitHub Desktop.
EFCore update row version field manually
void Update(Entity entity)
{
if (entity == null)
{
throw new ArgumentNullException(nameof(entity));
}
db.Entry(entity).State = EntityState.Detached;
var old = db.Entities.Find(entity.Id);
var oldEntry = db.Entry(old);
oldEntry.State = EntityState.Detached;
oldEntry.CurrentValues[nameof(entity.Version)] = entity.Version; // Row version field `Version`
oldEntry = db.Attach(old);
oldEntry.CurrentValues.SetValues(entity);
db.SaveChanges();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment