public void Dispose() { if (this.disposed) { return; } if (this.savedScope != null) { DbContextScope.SetAmbientScope(this.savedScope); this.savedScope = null; } this.disposed = true; }
/// <summary> /// Makes the provided 'dbContextScope' available as the the ambient scope via the CallContext. /// </summary> internal static void SetAmbientScope(DbContextScope newAmbientScope) { if (newAmbientScope == null) { throw new ArgumentNullException("newAmbientScope"); } var current = CallContext.LogicalGetData(AmbientDbContextScopeKey) as InstanceIdentifier; if (current == newAmbientScope.instanceIdentifier) { return; } // Store the new scope's instance identifier in the CallContext, making it the ambient scope CallContext.LogicalSetData(AmbientDbContextScopeKey, newAmbientScope.instanceIdentifier); // Keep track of this instance (or do nothing if we're already tracking it) DbContextScopeInstances.GetValue(newAmbientScope.instanceIdentifier, key => newAmbientScope); }
public TDbContext Get <TDbContext>() where TDbContext : DbContext { var ambientDbContextScope = DbContextScope.GetAmbientScope(); return(ambientDbContextScope?.DbContexts.Get <TDbContext>()); }
public DbContextReadOnlyScope(DbContextScopeOption joiningOption, IsolationLevel?isolationLevel, IDbContextFactory dbContextFactory = null) { this.internalScope = new DbContextScope(joiningOption : joiningOption, readOnly : true, isolationLevel : isolationLevel, dbContextFactory : dbContextFactory); }