Created
January 8, 2019 15:10
-
-
Save Zonciu/6569212b87a01f4c495feee2baed1f17 to your computer and use it in GitHub Desktop.
EFCore update row version field manually
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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