private DbContext Get(Type requestedType) { if (_disposed) { throw new ObjectDisposedException("DbContextCollection"); } if (!_initializedDbContexts.ContainsKey(requestedType)) { // First time we've been asked for this particular DbContext type. // Create one, cache it and start its database transaction if needed. var dbContext = _dbContextFactory != null ? _dbContextFactory.GetType().GetMethod("CreateDbContext").MakeGenericMethod(requestedType).Invoke(_dbContextFactory, null) as DbContext : Activator.CreateInstance(requestedType) as DbContext; _initializedDbContexts.Add(requestedType, dbContext); if (_readOnly) { dbContext.Configuration.AutoDetectChangesEnabled = false; } if (_isolationLevel.HasValue) { var tran = dbContext.Database.BeginTransaction(_isolationLevel.Value); _transactions.Add(dbContext, tran); } } return(_initializedDbContexts[requestedType] as DbContext); }