Created
June 14, 2014 09:06
-
-
Save Jawvig/c4775c825ede8075eb7c to your computer and use it in GitHub Desktop.
Temporary disabling of change tracking in Entity Framework
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
public sealed class NoChangeTracking : IDisposable | |
{ | |
private readonly DbContext _dbContext; | |
private readonly bool _initialAutoDetectChangesValue; | |
public NoChangeTracking(DbContext dbContext) | |
{ | |
if (dbContext == null) throw new ArgumentNullException("dbContext"); | |
_dbContext = dbContext; | |
_initialAutoDetectChangesValue = dbContext.Configuration.AutoDetectChangesEnabled; | |
SetChangeDetection(false); | |
} | |
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")] | |
public void Dispose() | |
{ | |
SetChangeDetection(_initialAutoDetectChangesValue); | |
} | |
private void SetChangeDetection(bool setting) | |
{ | |
_dbContext.Configuration.AutoDetectChangesEnabled = setting; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment