Skip to content

Instantly share code, notes, and snippets.

@stand-sure
Created September 19, 2024 21:07
Show Gist options
  • Save stand-sure/e45dad68dd3fe0a7a47a35b0cee6add3 to your computer and use it in GitHub Desktop.
Save stand-sure/e45dad68dd3fe0a7a47a35b0cee6add3 to your computer and use it in GitHub Desktop.
Row Version interceptor based on https://stackoverflow.com/a/78896330/93940
internal class RowVersionInterceptor : SaveChangesInterceptor
{
public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
{
DbContext dbContext = eventData.Context!;
foreach (EntityEntry entry in dbContext.ChangeTracker.Entries().Where(e => e.State is EntityState.Added or EntityState.Modified))
{
IEnumerable<PropertyEntry> concurrencyTokens = entry.Properties.Where(p => p.Metadata.IsConcurrencyToken);
foreach (PropertyEntry token in concurrencyTokens)
{
token.CurrentValue = new byte[] { 1 };
}
}
return base.SavingChanges(eventData, result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment