public virtual bool CompatibleWithModel( InternalContext internalContext, ModelHashCalculator modelHashCalculator, bool throwIfNoMetadata, DatabaseExistenceState existenceState = DatabaseExistenceState.Unknown) { if (internalContext.CodeFirstModel == null) { if (throwIfNoMetadata) { throw Error.Database_NonCodeFirstCompatibilityCheck(); } return(true); } VersionedModel model = internalContext.QueryForModel(existenceState); if (model != null) { return(internalContext.ModelMatches(model)); } string a = internalContext.QueryForModelHash(); if (a != null) { return(string.Equals(a, modelHashCalculator.Calculate(internalContext.CodeFirstModel), StringComparison.Ordinal)); } if (throwIfNoMetadata) { throw Error.Database_NoDatabaseMetadata(); } return(true); }
private HistoryRepository CreateHistoryRepository( DatabaseExistenceState existenceState = DatabaseExistenceState.Unknown) { this.DiscoverMigrationsConfiguration(); string connectionString = this.OriginalConnectionString; DbProviderFactory providerFactory = this.ProviderFactory; string contextKey = this._migrationsConfiguration().ContextKey; int?commandTimeout = this.CommandTimeout; Func <DbConnection, string, HistoryContext> historyContextFactory = this.HistoryContextFactory; object obj; if (this.DefaultSchema == null) { obj = (object)Enumerable.Empty <string>(); } else { obj = (object)new string[1] { this.DefaultSchema } }; DbContext owner = this.Owner; int num = (int)existenceState; return(new HistoryRepository(this, connectionString, providerFactory, contextKey, commandTimeout, historyContextFactory, (IEnumerable <string>)obj, owner, (DatabaseExistenceState)num)); }
internal DbMigrator( DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, bool calledByCreateDatabase) : base((MigratorBase)null) { Check.NotNull <DbMigrationsConfiguration>(configuration, nameof(configuration)); Check.NotNull <Type>(configuration.ContextType, "configuration.ContextType"); this._configuration = configuration; this._calledByCreateDatabase = calledByCreateDatabase; this._existenceState = existenceState; if (usersContext != null) { this._usersContextInfo = new DbContextInfo(usersContext, (Func <IDbDependencyResolver>)null); } else { this._usersContextInfo = configuration.TargetDatabase == null ? new DbContextInfo(configuration.ContextType) : new DbContextInfo(configuration.ContextType, configuration.TargetDatabase); if (!this._usersContextInfo.IsConstructible) { throw Error.ContextNotConstructible((object)configuration.ContextType); } } this._modelDiffer = this._configuration.ModelDiffer; DbContext context = usersContext ?? this._usersContextInfo.CreateInstance(); this._usersContext = context; try { this._migrationAssembly = new MigrationAssembly(this._configuration.MigrationsAssembly, this._configuration.MigrationsNamespace); this._currentModel = context.GetModel(); DbConnection connection = context.Database.Connection; this._providerFactory = DbProviderServices.GetProviderFactory(connection); this._defaultSchema = context.InternalContext.DefaultSchema ?? "dbo"; this._historyContextFactory = this._configuration.GetHistoryContextFactory(this._usersContextInfo.ConnectionProviderName); this._historyRepository = new HistoryRepository(context.InternalContext, this._usersContextInfo.ConnectionString, this._providerFactory, this._configuration.ContextKey, this._configuration.CommandTimeout, this._historyContextFactory, ((IEnumerable <string>) new string[1] { this._defaultSchema }).Concat <string>(this.GetHistorySchemas()), this._usersContext, this._existenceState); this._providerManifestToken = context.InternalContext.ModelProviderInfo != null ? context.InternalContext.ModelProviderInfo.ProviderManifestToken : DbConfiguration.DependencyResolver.GetService <IManifestTokenResolver>().ResolveManifestToken(connection); DbModelBuilder modelBuilder = context.InternalContext.CodeFirstModel.CachedModelBuilder; this._modificationCommandTreeGenerator = new Lazy <ModificationCommandTreeGenerator>((Func <ModificationCommandTreeGenerator>)(() => new ModificationCommandTreeGenerator(modelBuilder.BuildDynamicUpdateModel(new DbProviderInfo(this._usersContextInfo.ConnectionProviderName, this._providerManifestToken)), this.CreateConnection()))); DbInterceptionContext interceptionContext = new DbInterceptionContext().WithDbContext(this._usersContext); this._targetDatabase = Strings.LoggingTargetDatabaseFormat((object)DbInterception.Dispatch.Connection.GetDataSource(connection, interceptionContext), (object)DbInterception.Dispatch.Connection.GetDatabase(connection, interceptionContext), (object)this._usersContextInfo.ConnectionProviderName, this._usersContextInfo.ConnectionStringOrigin == DbConnectionStringOrigin.DbContextInfo ? (object)Strings.LoggingExplicit : (object)this._usersContextInfo.ConnectionStringOrigin.ToString()); this._legacyContextKey = context.InternalContext.DefaultContextKey; this._emptyModel = this.GetEmptyModel(); } finally { if (usersContext == null) { this._usersContext = (DbContext)null; context.Dispose(); } } }
internal void Create(DatabaseExistenceState existenceState) { if (existenceState == DatabaseExistenceState.Unknown) { if (this._internalContext.DatabaseOperations.Exists(this._internalContext.Connection, this._internalContext.CommandTimeout, new Lazy<StoreItemCollection>(new Func<StoreItemCollection>(this.CreateStoreItemCollection)))) throw Error.Database_DatabaseAlreadyExists((object) DbInterception.Dispatch.Connection.GetDatabase(this._internalContext.Connection, new DbInterceptionContext().WithDbContext(this._internalContext.Owner))); existenceState = DatabaseExistenceState.DoesNotExist; } using (ClonedObjectContext contextForDdlOps = this._internalContext.CreateObjectContextForDdlOps()) this._internalContext.CreateDatabase((ObjectContext) contextForDdlOps.ObjectContext, existenceState); }
public virtual VersionedModel QueryForModel(DatabaseExistenceState existenceState) { string migrationId; string productVersion; XDocument lastModel = this.CreateHistoryRepository(existenceState).GetLastModel(out migrationId, out productVersion, (string)null); if (lastModel == null) { return((VersionedModel)null); } return(new VersionedModel(lastModel, productVersion)); }
public HistoryRepository( InternalContext usersContext, string connectionString, DbProviderFactory providerFactory, string contextKey, int?commandTimeout, Func <DbConnection, string, HistoryContext> historyContextFactory, IEnumerable <string> schemas = null, DbContext contextForInterception = null, DatabaseExistenceState initialExistence = DatabaseExistenceState.Unknown) : base(usersContext, connectionString, providerFactory) { this._initialExistence = initialExistence; this._commandTimeout = commandTimeout; this._existingTransaction = usersContext.TryGetCurrentStoreTransaction(); this._schemas = ((IEnumerable <string>) new string[1] { "dbo" }).Concat <string>(schemas ?? Enumerable.Empty <string>()).Distinct <string>(); this._contextForInterception = contextForInterception; this._historyContextFactory = historyContextFactory; DbConnection connection = (DbConnection)null; try { connection = this.CreateConnection(); using (HistoryContext context = this.CreateContext(connection, (string)null)) { EntityType entityType = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItems <EntityType>(DataSpace.CSpace).Single <EntityType>((Func <EntityType, bool>)(et => EntityTypeExtensions.GetClrType(et) == typeof(HistoryRow))); int? maxLength = entityType.Properties.Single <EdmProperty>((Func <EdmProperty, bool>)(p => p.GetClrPropertyInfo().IsSameAs(HistoryRepository.MigrationIdProperty))).MaxLength; this._migrationIdMaxLength = maxLength.HasValue ? maxLength.Value : 150; maxLength = entityType.Properties.Single <EdmProperty>((Func <EdmProperty, bool>)(p => p.GetClrPropertyInfo().IsSameAs(HistoryRepository.ContextKeyProperty))).MaxLength; this._contextKeyMaxLength = maxLength.HasValue ? maxLength.Value : 300; } } finally { this.DisposeConnection(connection); } this._contextKey = contextKey.RestrictTo(this._contextKeyMaxLength); }
internal void Create(DatabaseExistenceState existenceState) { using (var clonedObjectContext = _internalContext.CreateObjectContextForDdlOps()) { if (existenceState == DatabaseExistenceState.Unknown) { if (_internalContext.DatabaseOperations.Exists(clonedObjectContext.ObjectContext)) { var interceptionContext = new DbInterceptionContext(); interceptionContext = interceptionContext.WithDbContext(_internalContext.Owner); interceptionContext = interceptionContext.WithObjectContext(clonedObjectContext.ObjectContext); throw Error.Database_DatabaseAlreadyExists( DbInterception.Dispatch.Connection.GetDatabase(_internalContext.Connection, interceptionContext)); } existenceState = DatabaseExistenceState.DoesNotExist; } _internalContext.CreateDatabase(clonedObjectContext.ObjectContext, existenceState); } }
public virtual bool CompatibleWithModel( InternalContext internalContext, ModelHashCalculator modelHashCalculator, bool throwIfNoMetadata, DatabaseExistenceState existenceState = DatabaseExistenceState.Unknown) { DebugCheck.NotNull(internalContext); DebugCheck.NotNull(modelHashCalculator); if (internalContext.CodeFirstModel == null) { if (throwIfNoMetadata) { throw Error.Database_NonCodeFirstCompatibilityCheck(); } return(true); } var model = internalContext.QueryForModel(existenceState); if (model != null) { return(internalContext.ModelMatches(model)); } // Migrations history was not found in the database so fall back to doing a model hash compare // to deal with databases created using EF 4.1 and 4.2. var databaseModelHash = internalContext.QueryForModelHash(); if (databaseModelHash == null) { if (throwIfNoMetadata) { throw Error.Database_NoDatabaseMetadata(); } return(true); } return(String.Equals( databaseModelHash, modelHashCalculator.Calculate(internalContext.CodeFirstModel), StringComparison.Ordinal)); }
public virtual bool CompatibleWithModel( InternalContext internalContext, ModelHashCalculator modelHashCalculator, bool throwIfNoMetadata, DatabaseExistenceState existenceState = DatabaseExistenceState.Unknown) { DebugCheck.NotNull(internalContext); DebugCheck.NotNull(modelHashCalculator); if (internalContext.CodeFirstModel == null) { if (throwIfNoMetadata) { throw Error.Database_NonCodeFirstCompatibilityCheck(); } return true; } var model = internalContext.QueryForModel(existenceState); if (model != null) { return internalContext.ModelMatches(model); } // Migrations history was not found in the database so fall back to doing a model hash compare // to deal with databases created using EF 4.1 and 4.2. var databaseModelHash = internalContext.QueryForModelHash(); if (databaseModelHash == null) { if (throwIfNoMetadata) { throw Error.Database_NoDatabaseMetadata(); } return true; } return String.Equals( databaseModelHash, modelHashCalculator.Calculate(internalContext.CodeFirstModel), StringComparison.Ordinal); }
internal DbMigrator(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, bool calledByCreateDatabase) : base(null) { Check.NotNull(configuration, "configuration"); Check.NotNull(configuration.ContextType, "configuration.ContextType"); _configuration = configuration; _calledByCreateDatabase = calledByCreateDatabase; _existenceState = existenceState; if (usersContext != null) { _usersContextInfo = new DbContextInfo(usersContext); } else { _usersContextInfo = configuration.TargetDatabase == null ? new DbContextInfo(configuration.ContextType) : new DbContextInfo(configuration.ContextType, configuration.TargetDatabase); if (!_usersContextInfo.IsConstructible) { throw Error.ContextNotConstructible(configuration.ContextType); } } _modelDiffer = _configuration.ModelDiffer; var context = usersContext ?? _usersContextInfo.CreateInstance(); _usersContext = context; try { _migrationAssembly = new MigrationAssembly( _configuration.MigrationsAssembly, _configuration.MigrationsNamespace); _currentModel = context.GetModel(); _connection = context.Database.Connection; _providerFactory = DbProviderServices.GetProviderFactory(_connection); _defaultSchema = context.InternalContext.DefaultSchema ?? EdmModelExtensions.DefaultSchema; _historyContextFactory = _configuration .GetHistoryContextFactory(_usersContextInfo.ConnectionProviderName); _historyRepository = new HistoryRepository( context.InternalContext, _usersContextInfo.ConnectionString, _providerFactory, _configuration.ContextKey, _configuration.CommandTimeout, _historyContextFactory, schemas: new[] { _defaultSchema }.Concat(GetHistorySchemas()), contextForInterception: _usersContext, initialExistence: _existenceState); _providerManifestToken = context.InternalContext.ModelProviderInfo != null ? context.InternalContext.ModelProviderInfo.ProviderManifestToken : DbConfiguration .DependencyResolver .GetService<IManifestTokenResolver>() .ResolveManifestToken(_connection); var modelBuilder = context.InternalContext.CodeFirstModel.CachedModelBuilder; _modificationCommandTreeGenerator = new Lazy<ModificationCommandTreeGenerator>( () => new ModificationCommandTreeGenerator( modelBuilder.BuildDynamicUpdateModel( new DbProviderInfo( _usersContextInfo.ConnectionProviderName, _providerManifestToken)), CreateConnection())); var interceptionContext = new DbInterceptionContext(); interceptionContext = interceptionContext.WithDbContext(_usersContext); _targetDatabase = Strings.LoggingTargetDatabaseFormat( DbInterception.Dispatch.Connection.GetDataSource(_connection, interceptionContext), DbInterception.Dispatch.Connection.GetDatabase(_connection, interceptionContext), _usersContextInfo.ConnectionProviderName, _usersContextInfo.ConnectionStringOrigin == DbConnectionStringOrigin.DbContextInfo ? Strings.LoggingExplicit : _usersContextInfo.ConnectionStringOrigin.ToString()); _legacyContextKey = context.InternalContext.DefaultContextKey; _emptyModel = GetEmptyModel(); } finally { if (usersContext == null) { _usersContext = null; context.Dispose(); } } }
internal bool CompatibleWithModel(bool throwIfNoMetadata, DatabaseExistenceState existenceState) { return(_internalContext.CompatibleWithModel(throwIfNoMetadata, existenceState)); }
public HistoryRepository( InternalContext usersContext, string connectionString, DbProviderFactory providerFactory, string contextKey, int?commandTimeout, Func <DbConnection, string, HistoryContext> historyContextFactory, IEnumerable <string> schemas = null, DbContext contextForInterception = null, DatabaseExistenceState initialExistence = DatabaseExistenceState.Unknown) : base(usersContext, connectionString, providerFactory) { DebugCheck.NotEmpty(contextKey); DebugCheck.NotNull(historyContextFactory); _initialExistence = initialExistence; _commandTimeout = commandTimeout; _existingTransaction = usersContext.TryGetCurrentStoreTransaction(); _schemas = new[] { EdmModelExtensions.DefaultSchema } .Concat(schemas ?? Enumerable.Empty <string>()) .Distinct(); _contextForInterception = contextForInterception; _historyContextFactory = historyContextFactory; DbConnection connection = null; try { connection = CreateConnection(); using (var context = CreateContext(connection)) { var historyRowEntity = ((IObjectContextAdapter)context).ObjectContext .MetadataWorkspace .GetItems <EntityType>(DataSpace.CSpace) .Single(et => et.GetClrType() == typeof(HistoryRow)); var maxLength = historyRowEntity .Properties .Single(p => p.GetClrPropertyInfo().IsSameAs(MigrationIdProperty)) .MaxLength; _migrationIdMaxLength = maxLength.HasValue ? maxLength.Value : HistoryContext.MigrationIdMaxLength; maxLength = historyRowEntity .Properties .Single(p => p.GetClrPropertyInfo().IsSameAs(ContextKeyProperty)) .MaxLength; _contextKeyMaxLength = maxLength.HasValue ? maxLength.Value : HistoryContext.ContextKeyMaxLength; } } finally { DisposeConnection(connection); } _contextKey = contextKey.RestrictTo(_contextKeyMaxLength); }
public virtual bool CompatibleWithModel( bool throwIfNoMetadata, DatabaseExistenceState existenceState) { return(new ModelCompatibilityChecker().CompatibleWithModel(this, new ModelHashCalculator(), throwIfNoMetadata, existenceState)); }
public virtual void CreateDatabase( ObjectContext objectContext, DatabaseExistenceState existenceState) { new DatabaseCreator().CreateDatabase(this, (Func <DbMigrationsConfiguration, DbContext, MigratorBase>)((config, context) => (MigratorBase) new DbMigrator(config, context, existenceState, true)), objectContext); }
public HistoryRepository( InternalContext usersContext, string connectionString, DbProviderFactory providerFactory, string contextKey, int? commandTimeout, Func<DbConnection, string, HistoryContext> historyContextFactory, IEnumerable<string> schemas = null, DbContext contextForInterception = null, DatabaseExistenceState initialExistence = DatabaseExistenceState.Unknown) : base(usersContext, connectionString, providerFactory) { DebugCheck.NotEmpty(contextKey); DebugCheck.NotNull(historyContextFactory); _initialExistence = initialExistence; _commandTimeout = commandTimeout; _existingTransaction = usersContext.TryGetCurrentStoreTransaction(); _schemas = new[] { EdmModelExtensions.DefaultSchema } .Concat(schemas ?? Enumerable.Empty<string>()) .Distinct(); _contextForInterception = contextForInterception; _historyContextFactory = historyContextFactory; DbConnection connection = null; try { connection = CreateConnection(); using (var context = CreateContext(connection)) { var historyRowEntity = ((IObjectContextAdapter)context).ObjectContext .MetadataWorkspace .GetItems<EntityType>(DataSpace.CSpace) .Single(et => et.GetClrType() == typeof(HistoryRow)); var maxLength = historyRowEntity .Properties .Single(p => p.GetClrPropertyInfo().IsSameAs(MigrationIdProperty)) .MaxLength; _migrationIdMaxLength = maxLength.HasValue ? maxLength.Value : HistoryContext.MigrationIdMaxLength; maxLength = historyRowEntity .Properties .Single(p => p.GetClrPropertyInfo().IsSameAs(ContextKeyProperty)) .MaxLength; _contextKeyMaxLength = maxLength.HasValue ? maxLength.Value : HistoryContext.ContextKeyMaxLength; } } finally { DisposeConnection(connection); } _contextKey = contextKey.RestrictTo(_contextKeyMaxLength); }
internal DbMigrator(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState) : base(null) { Check.NotNull(configuration, "configuration"); Check.NotNull(configuration.ContextType, "configuration.ContextType"); _configuration = configuration; _calledByCreateDatabase = usersContext != null; _existenceState = existenceState; if (_calledByCreateDatabase) { _usersContextInfo = new DbContextInfo(usersContext); } else { _usersContextInfo = configuration.TargetDatabase == null ? new DbContextInfo(configuration.ContextType) : new DbContextInfo(configuration.ContextType, configuration.TargetDatabase); if (!_usersContextInfo.IsConstructible) { throw Error.ContextNotConstructible(configuration.ContextType); } } _modelDiffer = _configuration.ModelDiffer; var context = usersContext ?? _usersContextInfo.CreateInstance(); _contextForInterception = context; try { _migrationAssembly = new MigrationAssembly( _configuration.MigrationsAssembly, _configuration.MigrationsNamespace); _currentModel = context.GetModel(); var connection = context.Database.Connection; _providerFactory = DbProviderServices.GetProviderFactory(connection); _defaultSchema = context.InternalContext.DefaultSchema ?? EdmModelExtensions.DefaultSchema; _historyContextFactory = _configuration .GetHistoryContextFactory(_usersContextInfo.ConnectionProviderName); _historyRepository = new HistoryRepository( _usersContextInfo.ConnectionString, _providerFactory, _configuration.ContextKey, _configuration.CommandTimeout, _historyContextFactory, schemas: new[] { _defaultSchema }.Concat(GetHistorySchemas()), contextForInterception: _contextForInterception, initialExistence: _existenceState); _providerManifestToken = context.InternalContext.ModelProviderInfo != null ? context.InternalContext.ModelProviderInfo.ProviderManifestToken : DbConfiguration .DependencyResolver .GetService <IManifestTokenResolver>() .ResolveManifestToken(connection); var modelBuilder = context.InternalContext.CodeFirstModel.CachedModelBuilder; _modificationCommandTreeGenerator = new Lazy <ModificationCommandTreeGenerator>( () => new ModificationCommandTreeGenerator( modelBuilder.BuildDynamicUpdateModel( new DbProviderInfo( _usersContextInfo.ConnectionProviderName, _providerManifestToken)), CreateConnection())); var interceptionContext = new DbInterceptionContext(); interceptionContext = interceptionContext.WithDbContext(_contextForInterception); _targetDatabase = Strings.LoggingTargetDatabaseFormat( DbInterception.Dispatch.Connection.GetDataSource(connection, interceptionContext), DbInterception.Dispatch.Connection.GetDatabase(connection, interceptionContext), _usersContextInfo.ConnectionProviderName, _usersContextInfo.ConnectionStringOrigin == DbConnectionStringOrigin.DbContextInfo ? Strings.LoggingExplicit : _usersContextInfo.ConnectionStringOrigin.ToString()); _legacyContextKey = context.InternalContext.DefaultContextKey; _emptyModel = GetEmptyModel(); } finally { if (usersContext == null) { _contextForInterception = null; context.Dispose(); } } }