Created
September 19, 2024 21:07
-
-
Save stand-sure/e45dad68dd3fe0a7a47a35b0cee6add3 to your computer and use it in GitHub Desktop.
Row Version interceptor based on https://stackoverflow.com/a/78896330/93940
This file contains hidden or 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
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