/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public Migrator( [NotNull] IMigrationsAssembly migrationsAssembly, [NotNull] IHistoryRepository historyRepository, [NotNull] IDatabaseCreator databaseCreator, [NotNull] IMigrationsSqlGenerator migrationsSqlGenerator, [NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder, [NotNull] IMigrationCommandExecutor migrationCommandExecutor, [NotNull] IRelationalConnection connection, [NotNull] ISqlGenerationHelper sqlGenerationHelper, [NotNull] IInterceptingLogger <LoggerCategory.Migrations> logger, [NotNull] IDatabaseProvider databaseProvider) { Check.NotNull(migrationsAssembly, nameof(migrationsAssembly)); Check.NotNull(historyRepository, nameof(historyRepository)); Check.NotNull(databaseCreator, nameof(databaseCreator)); Check.NotNull(migrationsSqlGenerator, nameof(migrationsSqlGenerator)); Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder)); Check.NotNull(migrationCommandExecutor, nameof(migrationCommandExecutor)); Check.NotNull(connection, nameof(connection)); Check.NotNull(sqlGenerationHelper, nameof(sqlGenerationHelper)); Check.NotNull(logger, nameof(logger)); Check.NotNull(databaseProvider, nameof(databaseProvider)); _migrationsAssembly = migrationsAssembly; _historyRepository = historyRepository; _databaseCreator = (IRelationalDatabaseCreator)databaseCreator; _migrationsSqlGenerator = migrationsSqlGenerator; _rawSqlCommandBuilder = rawSqlCommandBuilder; _migrationCommandExecutor = migrationCommandExecutor; _connection = connection; _sqlGenerationHelper = sqlGenerationHelper; _logger = logger; _activeProvider = databaseProvider.InvariantName; }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public Migrator( IMigrationsAssembly migrationsAssembly, IHistoryRepository historyRepository, IDatabaseCreator databaseCreator, IMigrationsSqlGenerator migrationsSqlGenerator, IRawSqlCommandBuilder rawSqlCommandBuilder, IMigrationCommandExecutor migrationCommandExecutor, IRelationalConnection connection, ISqlGenerationHelper sqlGenerationHelper, ICurrentDbContext currentContext, IModelRuntimeInitializer modelRuntimeInitializer, IDiagnosticsLogger <DbLoggerCategory.Migrations> logger, IRelationalCommandDiagnosticsLogger commandLogger, IDatabaseProvider databaseProvider) { _migrationsAssembly = migrationsAssembly; _historyRepository = historyRepository; _databaseCreator = (IRelationalDatabaseCreator)databaseCreator; _migrationsSqlGenerator = migrationsSqlGenerator; _rawSqlCommandBuilder = rawSqlCommandBuilder; _migrationCommandExecutor = migrationCommandExecutor; _connection = connection; _sqlGenerationHelper = sqlGenerationHelper; _currentContext = currentContext; _modelRuntimeInitializer = modelRuntimeInitializer; _logger = logger; _commandLogger = commandLogger; _activeProvider = databaseProvider.Name; }
/// <summary> /// <para> /// Creates the service dependencies parameter object for a <see cref="HistoryRepository" />. /// </para> /// <para> /// Do not call this constructor directly from either provider or application code as it may change /// as new dependencies are added. Instead, use this type in your constructor so that an instance /// will be created and injected automatically by the dependency injection container. To create /// an instance with some dependent services replaced, first resolve the object from the dependency /// injection container, then replace selected services using the 'With...' methods. Do not call /// the constructor at any point in this process. /// </para> /// <para> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </para> /// </summary> /// <param name="databaseCreator"> The database creator. </param> /// <param name="rawSqlCommandBuilder"> A command builder for building raw SQL commands. </param> /// <param name="connection"> The connection to the database. </param> /// <param name="options"> Options for the current context instance. </param> /// <param name="modelDiffer"> The model differ. </param> /// <param name="migrationsSqlGenerator"> The SQL generator for Migrations operations. </param> /// <param name="sqlGenerationHelper"> Helpers for generating update SQL. </param> /// <param name="coreConventionSetBuilder"> The core convention set to use when creating the model. </param> /// <param name="conventionSetBuilders"> The convention sets to use when creating the model. </param> public HistoryRepositoryDependencies( [NotNull] IRelationalDatabaseCreator databaseCreator, [NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder, [NotNull] IRelationalConnection connection, [NotNull] IDbContextOptions options, [NotNull] IMigrationsModelDiffer modelDiffer, [NotNull] IMigrationsSqlGenerator migrationsSqlGenerator, [NotNull] ISqlGenerationHelper sqlGenerationHelper, [NotNull] ICoreConventionSetBuilder coreConventionSetBuilder, [NotNull] IEnumerable <IConventionSetBuilder> conventionSetBuilders) { Check.NotNull(databaseCreator, nameof(databaseCreator)); Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder)); Check.NotNull(connection, nameof(connection)); Check.NotNull(options, nameof(options)); Check.NotNull(modelDiffer, nameof(modelDiffer)); Check.NotNull(migrationsSqlGenerator, nameof(migrationsSqlGenerator)); Check.NotNull(sqlGenerationHelper, nameof(sqlGenerationHelper)); Check.NotNull(coreConventionSetBuilder, nameof(coreConventionSetBuilder)); Check.NotNull(conventionSetBuilders, nameof(conventionSetBuilders)); DatabaseCreator = databaseCreator; RawSqlCommandBuilder = rawSqlCommandBuilder; Connection = connection; Options = options; ModelDiffer = modelDiffer; MigrationsSqlGenerator = migrationsSqlGenerator; SqlGenerationHelper = sqlGenerationHelper; CoreConventionSetBuilder = coreConventionSetBuilder; ConventionSetBuilder = new CompositeConventionSetBuilder((IReadOnlyList <IConventionSetBuilder>)conventionSetBuilders); }
/// <summary> /// <para> /// Creates the service dependencies parameter object for a <see cref="HistoryRepository" />. /// </para> /// <para> /// Do not call this constructor directly from either provider or application code as it may change /// as new dependencies are added. Instead, use this type in your constructor so that an instance /// will be created and injected automatically by the dependency injection container. To create /// an instance with some dependent services replaced, first resolve the object from the dependency /// injection container, then replace selected services using the 'With...' methods. Do not call /// the constructor at any point in this process. /// </para> /// <para> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </para> /// </summary> /// <param name="databaseCreator"> The database creator. </param> /// <param name="rawSqlCommandBuilder"> A command builder for building raw SQL commands. </param> /// <param name="connection"> The connection to the database. </param> /// <param name="options"> Options for the current context instance. </param> /// <param name="modelDiffer"> The model differ. </param> /// <param name="migrationsSqlGenerator"> The SQL generator for Migrations operations. </param> /// <param name="sqlGenerationHelper"> Helpers for generating update SQL. </param> public HistoryRepositoryDependencies( [NotNull] IRelationalDatabaseCreator databaseCreator, [NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder, [NotNull] IRelationalConnection connection, [NotNull] IDbContextOptions options, [NotNull] IMigrationsModelDiffer modelDiffer, [NotNull] IMigrationsSqlGenerator migrationsSqlGenerator, [NotNull] ISqlGenerationHelper sqlGenerationHelper) { Check.NotNull(databaseCreator, nameof(databaseCreator)); Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder)); Check.NotNull(connection, nameof(connection)); Check.NotNull(options, nameof(options)); Check.NotNull(modelDiffer, nameof(modelDiffer)); Check.NotNull(migrationsSqlGenerator, nameof(migrationsSqlGenerator)); Check.NotNull(sqlGenerationHelper, nameof(sqlGenerationHelper)); DatabaseCreator = databaseCreator; RawSqlCommandBuilder = rawSqlCommandBuilder; Connection = connection; Options = options; ModelDiffer = modelDiffer; MigrationsSqlGenerator = migrationsSqlGenerator; SqlGenerationHelper = sqlGenerationHelper; }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public Migrator( [NotNull] IMigrationsAssembly migrationsAssembly, [NotNull] IHistoryRepository historyRepository, [NotNull] IDatabaseCreator databaseCreator, [NotNull] IMigrationsSqlGenerator migrationsSqlGenerator, [NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder, [NotNull] IMigrationCommandExecutor migrationCommandExecutor, [NotNull] IRelationalConnection connection, [NotNull] ISqlGenerationHelper sqlGenerationHelper, [NotNull] ILogger<Migrator> logger, [NotNull] IDatabaseProviderServices providerServices) { Check.NotNull(migrationsAssembly, nameof(migrationsAssembly)); Check.NotNull(historyRepository, nameof(historyRepository)); Check.NotNull(databaseCreator, nameof(databaseCreator)); Check.NotNull(migrationsSqlGenerator, nameof(migrationsSqlGenerator)); Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder)); Check.NotNull(migrationCommandExecutor, nameof(migrationCommandExecutor)); Check.NotNull(connection, nameof(connection)); Check.NotNull(sqlGenerationHelper, nameof(sqlGenerationHelper)); Check.NotNull(logger, nameof(logger)); Check.NotNull(providerServices, nameof(providerServices)); _migrationsAssembly = migrationsAssembly; _historyRepository = historyRepository; _databaseCreator = (IRelationalDatabaseCreator)databaseCreator; _migrationsSqlGenerator = migrationsSqlGenerator; _rawSqlCommandBuilder = rawSqlCommandBuilder; _migrationCommandExecutor = migrationCommandExecutor; _connection = connection; _sqlGenerationHelper = sqlGenerationHelper; _logger = logger; _activeProvider = providerServices.InvariantName; }
public Migrator( [NotNull] IMigrationsAssembly migrationsAssembly, [NotNull] IHistoryRepository historyRepository, [NotNull] IDatabaseCreator databaseCreator, [NotNull] IMigrationsSqlGenerator migrationsSqlGenerator, [NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder, [NotNull] IRelationalConnection connection, [NotNull] ISqlGenerationHelper sqlGenerationHelper, [NotNull] ILogger <Migrator> logger, [NotNull] IDatabaseProviderServices providerServices) { Check.NotNull(migrationsAssembly, nameof(migrationsAssembly)); Check.NotNull(historyRepository, nameof(historyRepository)); Check.NotNull(databaseCreator, nameof(databaseCreator)); Check.NotNull(migrationsSqlGenerator, nameof(migrationsSqlGenerator)); Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder)); Check.NotNull(connection, nameof(connection)); Check.NotNull(sqlGenerationHelper, nameof(sqlGenerationHelper)); Check.NotNull(logger, nameof(logger)); Check.NotNull(providerServices, nameof(providerServices)); _migrationsAssembly = migrationsAssembly; _historyRepository = historyRepository; _databaseCreator = (IRelationalDatabaseCreator)databaseCreator; _migrationsSqlGenerator = migrationsSqlGenerator; _rawSqlCommandBuilder = rawSqlCommandBuilder; _connection = connection; _sqlGenerationHelper = sqlGenerationHelper; _logger = logger; _activeProvider = providerServices.InvariantName; }
public Task StartAsync(CancellationToken cancellationToken) { // Load Open Banking Connector configuration options OpenBankingConnectorSettings obcSettings = _obcSettingsProvider.GetSettings(); // Ensure DB exists using IServiceScope scope = _serviceScopeFactory.CreateScope(); using BaseDbContext context = scope.ServiceProvider.GetRequiredService <BaseDbContext>(); if (obcSettings.Database.ProcessedEnsureDbCreated) { // Create DB if configured to do so and DB doesn't exist context.Database.EnsureCreated(); } else { // Throw exception if DB doesn't exist IRelationalDatabaseCreator creator = context.Database.GetService <IRelationalDatabaseCreator>(); if (!creator.Exists()) { throw new ApplicationException( "No database found. Run 'dotnet ef database update' in OpenBanking.WebApp.Connector.Sample root folder to create test DB."); } } return(Task.CompletedTask); }
/// <summary> /// <para> /// Creates the service dependencies parameter object for a <see cref="HistoryRepository" />. /// </para> /// <para> /// Do not call this constructor directly from either provider or application code as it may change /// as new dependencies are added. Instead, use this type in your constructor so that an instance /// will be created and injected automatically by the dependency injection container. To create /// an instance with some dependent services replaced, first resolve the object from the dependency /// injection container, then replace selected services using the 'With...' methods. Do not call /// the constructor at any point in this process. /// </para> /// <para> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </para> /// </summary> /// <param name="databaseCreator"> The database creator. </param> /// <param name="rawSqlCommandBuilder"> A command builder for building raw SQL commands. </param> /// <param name="connection"> The connection to the database. </param> /// <param name="options"> Options for the current context instance. </param> /// <param name="modelDiffer"> The model differ. </param> /// <param name="migrationsSqlGenerator"> The SQL generator for Migrations operations. </param> /// <param name="sqlGenerationHelper"> Helpers for generating update SQL. </param> /// <param name="conventionSetBuilder"> The convention set to use when creating the model. </param> /// <param name="typeMappingSource"> The type mapper. </param> /// <param name="modelLogger"> The logger for model building events. </param> public HistoryRepositoryDependencies( [NotNull] IRelationalDatabaseCreator databaseCreator, [NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder, [NotNull] IRelationalConnection connection, [NotNull] IDbContextOptions options, [NotNull] IMigrationsModelDiffer modelDiffer, [NotNull] IMigrationsSqlGenerator migrationsSqlGenerator, [NotNull] ISqlGenerationHelper sqlGenerationHelper, [NotNull] IConventionSetBuilder conventionSetBuilder, [NotNull] IRelationalTypeMappingSource typeMappingSource, [NotNull] IDiagnosticsLogger <DbLoggerCategory.Model> modelLogger) { Check.NotNull(databaseCreator, nameof(databaseCreator)); Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder)); Check.NotNull(connection, nameof(connection)); Check.NotNull(options, nameof(options)); Check.NotNull(modelDiffer, nameof(modelDiffer)); Check.NotNull(migrationsSqlGenerator, nameof(migrationsSqlGenerator)); Check.NotNull(sqlGenerationHelper, nameof(sqlGenerationHelper)); Check.NotNull(conventionSetBuilder, nameof(conventionSetBuilder)); Check.NotNull(typeMappingSource, nameof(typeMappingSource)); Check.NotNull(modelLogger, nameof(modelLogger)); DatabaseCreator = databaseCreator; RawSqlCommandBuilder = rawSqlCommandBuilder; Connection = connection; Options = options; ModelDiffer = modelDiffer; MigrationsSqlGenerator = migrationsSqlGenerator; SqlGenerationHelper = sqlGenerationHelper; ConventionSetBuilder = conventionSetBuilder; TypeMappingSource = typeMappingSource; ModelLogger = modelLogger; }
public Migrator( [NotNull] IMigrationsAssembly migrationAssembly, [NotNull] IHistoryRepository historyRepository, [NotNull] IDatabaseCreator databaseCreator, [NotNull] IMigrationsSqlGenerator sqlGenerator, [NotNull] ISqlStatementExecutor executor, [NotNull] IRelationalConnection connection, [NotNull] IUpdateSqlGenerator sql, [NotNull] ILoggerFactory loggerFactory) { Check.NotNull(migrationAssembly, nameof(migrationAssembly)); Check.NotNull(historyRepository, nameof(historyRepository)); Check.NotNull(databaseCreator, nameof(databaseCreator)); Check.NotNull(sqlGenerator, nameof(sqlGenerator)); Check.NotNull(executor, nameof(executor)); Check.NotNull(connection, nameof(connection)); Check.NotNull(sql, nameof(sql)); Check.NotNull(loggerFactory, nameof(loggerFactory)); _migrationAssembly = migrationAssembly; _historyRepository = historyRepository; _databaseCreator = (IRelationalDatabaseCreator)databaseCreator; _sqlGenerator = sqlGenerator; _executor = executor; _connection = connection; _sql = sql; _logger = new LazyRef <ILogger>(loggerFactory.CreateLogger <Migrator>); }
public Migrator( [NotNull] IMigrationsAssembly migrationAssembly, [NotNull] IHistoryRepository historyRepository, [NotNull] IDatabaseCreator databaseCreator, [NotNull] IMigrationsSqlGenerator sqlGenerator, [NotNull] ISqlStatementExecutor executor, [NotNull] IRelationalConnection connection, [NotNull] IUpdateSqlGenerator sql, [NotNull] ILoggerFactory loggerFactory) { Check.NotNull(migrationAssembly, nameof(migrationAssembly)); Check.NotNull(historyRepository, nameof(historyRepository)); Check.NotNull(databaseCreator, nameof(databaseCreator)); Check.NotNull(sqlGenerator, nameof(sqlGenerator)); Check.NotNull(executor, nameof(executor)); Check.NotNull(connection, nameof(connection)); Check.NotNull(sql, nameof(sql)); Check.NotNull(loggerFactory, nameof(loggerFactory)); _migrationAssembly = migrationAssembly; _historyRepository = historyRepository; _databaseCreator = (IRelationalDatabaseCreator)databaseCreator; _sqlGenerator = sqlGenerator; _executor = executor; _connection = connection; _sql = sql; _logger = new LazyRef<ILogger>(loggerFactory.CreateLogger<Migrator>); }
public RelationalDatabase( IDataContextService dbContext, IRelationalConnection connection, IRelationalDatabaseCreator creator) { _dbContext = dbContext; Connection = connection; Creator = creator; }
/// <summary> /// Clones this dependency parameter object with one service replaced. /// </summary> /// <param name="databaseCreator"> A replacement for the current dependency of this type. </param> /// <returns> A new parameter object with the given service replaced. </returns> public HistoryRepositoryDependencies With([NotNull] IRelationalDatabaseCreator databaseCreator) => new HistoryRepositoryDependencies( databaseCreator, RawSqlCommandBuilder, Connection, Options, ModelDiffer, MigrationsSqlGenerator, SqlGenerationHelper);
public static void InitializeAudit <T>(this T context, Func <DbContextOptionsBuilder, DbContextOptionsBuilder> builder) where T : DbContext { InitializeAuditedTablesMetadataCache(context); using (AuditableContext auditContext = new AuditableContext(builder(new DbContextOptionsBuilder()).Options)) { IRelationalDatabaseCreator creator = auditContext.GetService <IRelationalDatabaseCreator>(); creator.CreateTables(); } }
/// <summary> /// Clones this dependency parameter object with one service replaced. /// </summary> /// <param name="databaseCreator"> A replacement for the current dependency of this type. </param> /// <returns> A new parameter object with the given service replaced. </returns> public HistoryRepositoryDependencies With([NotNull] IRelationalDatabaseCreator databaseCreator) => new HistoryRepositoryDependencies( databaseCreator, RawSqlCommandBuilder, Connection, Options, ModelDiffer, MigrationsSqlGenerator, SqlGenerationHelper, ConventionSetBuilder, TypeMappingSource, CurrentContext, ModelLogger, CommandLogger);
protected HistoryRepository( [NotNull] IDatabaseCreator databaseCreator, [NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder, [NotNull] IRelationalConnection connection, [NotNull] IDbContextOptions options, [NotNull] IMigrationsModelDiffer modelDiffer, [NotNull] IMigrationsSqlGenerator migrationsSqlGenerator, [NotNull] IRelationalAnnotationProvider annotations, [NotNull] ISqlGenerationHelper sqlGenerationHelper) { Check.NotNull(databaseCreator, nameof(databaseCreator)); Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder)); Check.NotNull(connection, nameof(connection)); Check.NotNull(options, nameof(options)); Check.NotNull(modelDiffer, nameof(modelDiffer)); Check.NotNull(migrationsSqlGenerator, nameof(migrationsSqlGenerator)); Check.NotNull(annotations, nameof(annotations)); Check.NotNull(sqlGenerationHelper, nameof(sqlGenerationHelper)); _databaseCreator = (IRelationalDatabaseCreator)databaseCreator; _rawSqlCommandBuilder = rawSqlCommandBuilder; _connection = connection; _modelDiffer = modelDiffer; _migrationsSqlGenerator = migrationsSqlGenerator; SqlGenerationHelper = sqlGenerationHelper; var relationalOptions = RelationalOptionsExtension.Extract(options); TableName = relationalOptions?.MigrationsHistoryTableName ?? DefaultTableName; TableSchema = relationalOptions.MigrationsHistoryTableSchema; _model = new LazyRef <IModel>( () => { var modelBuilder = new ModelBuilder(new ConventionSet()); modelBuilder.Entity <HistoryRow>( x => { ConfigureTable(x); x.ToTable(TableName, TableSchema); }); return(modelBuilder.Model); }); var entityType = new LazyRef <IEntityType>(() => _model.Value.FindEntityType(typeof(HistoryRow))); _migrationIdColumnName = new LazyRef <string>( () => annotations.For(entityType.Value.FindProperty(nameof(HistoryRow.MigrationId))).ColumnName); _productVersionColumnName = new LazyRef <string>( () => annotations.For(entityType.Value.FindProperty(nameof(HistoryRow.ProductVersion))).ColumnName); }
/// <summary> /// Constructs a new Sqlite relational database service. /// </summary> /// <param name="dbContext">Injected DbContext service which contains a reference to the actual DbContext.</param> /// <param name="connection">Injected Sqlite connection service.</param> /// <param name="creator">Injected Sqlite database creator service.</param> public SqliteRelationalDatabase( IDataContextService dbContext, IRelationalConnection connection, IRelationalDatabaseCreator creator) : base(dbContext, connection, creator) { // The Sqlite database service maintains an open database connection for the lifetime of the DbContext. // We use the connection state status to determine if the connection should be closed when the database is disposed. Trace.WriteLine("SqliteRelationalDatabase constructor: Opening connection."); _wasConnectionOpened = Connection.Open(); Trace.WriteLine("Connection opened internally is " + _wasConnectionOpened.ToString()); }
public HistoryRepository( [NotNull] IDatabaseCreator databaseCreator, [NotNull] ISqlStatementExecutor executor, [NotNull] IRelationalConnection connection, [NotNull] IDbContextOptions options, [NotNull] IMigrationsModelDiffer modelDiffer, [NotNull] IMigrationsSqlGenerator migrationsSqlGenerator, [NotNull] IRelationalAnnotationProvider annotations, [NotNull] ISqlGenerator sqlGenerator) { Check.NotNull(databaseCreator, nameof(databaseCreator)); Check.NotNull(executor, nameof(executor)); Check.NotNull(connection, nameof(connection)); Check.NotNull(options, nameof(options)); Check.NotNull(modelDiffer, nameof(modelDiffer)); Check.NotNull(migrationsSqlGenerator, nameof(migrationsSqlGenerator)); Check.NotNull(annotations, nameof(annotations)); Check.NotNull(sqlGenerator, nameof(sqlGenerator)); _databaseCreator = (IRelationalDatabaseCreator)databaseCreator; _executor = executor; _connection = connection; _modelDiffer = modelDiffer; _migrationsSqlGenerator = migrationsSqlGenerator; SqlGenerator = sqlGenerator; var relationalOptions = RelationalOptionsExtension.Extract(options); TableName = relationalOptions?.MigrationsHistoryTableName ?? DefaultTableName; TableSchema = relationalOptions.MigrationsHistoryTableSchema; _model = new LazyRef<IModel>( () => { var modelBuilder = new ModelBuilder(new ConventionSet()); modelBuilder.Entity<HistoryRow>( x => { ConfigureTable(x); x.ToTable(TableName, TableSchema); }); return modelBuilder.Model; }); var entityType = new LazyRef<IEntityType>(() => _model.Value.GetEntityType(typeof(HistoryRow))); _migrationIdColumnName = new LazyRef<string>( () => annotations.For(entityType.Value.FindProperty(nameof(HistoryRow.MigrationId))).ColumnName); _productVersionColumnName = new LazyRef<string>( () => annotations.For(entityType.Value.FindProperty(nameof(HistoryRow.ProductVersion))).ColumnName); }
public NpgsqlHistoryRepository( [NotNull] NpgsqlDatabaseConnection connection, [NotNull] IRelationalDatabaseCreator creator, [NotNull] DbContext context, [NotNull] NpgsqlUpdateSqlGenerator sqlGenerator) { Check.NotNull(connection, nameof(connection)); Check.NotNull(creator, nameof(creator)); Check.NotNull(context, nameof(context)); Check.NotNull(sqlGenerator, nameof(sqlGenerator)); _connection = connection; _creator = creator; _contextType = context.GetType(); _sql = sqlGenerator; }
public SqlServerHistoryRepository( [NotNull] ISqlServerConnection connection, [NotNull] IRelationalDatabaseCreator creator, [NotNull] DbContext context, [NotNull] ISqlServerUpdateSqlGenerator sqlGenerator) { Check.NotNull(connection, nameof(connection)); Check.NotNull(creator, nameof(creator)); Check.NotNull(context, nameof(context)); Check.NotNull(sqlGenerator, nameof(sqlGenerator)); _connection = connection; _creator = creator; _contextType = context.GetType(); _sql = sqlGenerator; }
/// <summary> /// Creates a new instance of <see cref="DataMigrator"/>. /// </summary> public DataMigrator( IHistoryRepository historyRepository, IDatabaseCreator databaseCreator, IMigrationsSqlGenerator migrationsSqlGenerator, IRawSqlCommandBuilder rawSqlCommandBuilder, IRelationalConnection connection, ILoggerFactory loggerFactory, IServiceProvider serviceProvider) { _historyRepository = historyRepository; _databaseCreator = (IRelationalDatabaseCreator)databaseCreator; _migrationsSqlGenerator = migrationsSqlGenerator; _rawSqlCommandBuilder = rawSqlCommandBuilder; _connection = connection; _serviceProvider = serviceProvider; _logger = loggerFactory.CreateLogger<DataMigrator>(); }
/// <summary> /// Executes pending migrations and update procedures. /// </summary> public void ApplayMigrations() { if (_databaseOptions.Type == DatabaseTypes.InMemory) { return; } _logger.LogInformation("===== Release Management ====="); try { _logger.LogInformation($"Hosting env: {_hostingEnvironment}"); IRelationalDatabaseCreator relationalDatabaseCreator = _context.Database.GetService <IDatabaseCreator>() as IRelationalDatabaseCreator; if (!relationalDatabaseCreator.Exists()) { _logger.LogInformation($"Creating new Database"); relationalDatabaseCreator.Create(); _logger.LogInformation($"New Database was created"); } List <IUpdate> updates = AllUpdates(); _logger.LogInformation($"All updates(also already applied): {string.Join(", ", updates)}"); List <string> applyedMigrations = _context.Database .GetAppliedMigrations() .ToList(); foreach (var update in updates) { if (update.ShouldExecute(applyedMigrations)) { PerformUpdateInTransaction(update); } } _logger.LogInformation("===== Release Management Finished ====="); } catch (Exception ex) { _logger.LogError($"Error during ReleaseManagment. {ex}"); throw new Exception($"ReleaseManagment Error. {ex.Message}"); } }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public Migrator( [NotNull] IMigrationsAssembly migrationsAssembly, [NotNull] IHistoryRepository historyRepository, [NotNull] IDatabaseCreator databaseCreator, [NotNull] IMigrationsSqlGenerator migrationsSqlGenerator, [NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder, [NotNull] IMigrationCommandExecutor migrationCommandExecutor, [NotNull] IRelationalConnection connection, [NotNull] ISqlGenerationHelper sqlGenerationHelper, [NotNull] ICurrentDbContext currentContext, [NotNull] IConventionSetBuilder conventionSetBuilder, [NotNull] IDiagnosticsLogger <DbLoggerCategory.Migrations> logger, [NotNull] IDiagnosticsLogger <DbLoggerCategory.Database.Command> commandLogger, [NotNull] IDatabaseProvider databaseProvider) { Check.NotNull(migrationsAssembly, nameof(migrationsAssembly)); Check.NotNull(historyRepository, nameof(historyRepository)); Check.NotNull(databaseCreator, nameof(databaseCreator)); Check.NotNull(migrationsSqlGenerator, nameof(migrationsSqlGenerator)); Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder)); Check.NotNull(migrationCommandExecutor, nameof(migrationCommandExecutor)); Check.NotNull(connection, nameof(connection)); Check.NotNull(sqlGenerationHelper, nameof(sqlGenerationHelper)); Check.NotNull(currentContext, nameof(currentContext)); Check.NotNull(conventionSetBuilder, nameof(conventionSetBuilder)); Check.NotNull(logger, nameof(logger)); Check.NotNull(commandLogger, nameof(commandLogger)); Check.NotNull(databaseProvider, nameof(databaseProvider)); _migrationsAssembly = migrationsAssembly; _historyRepository = historyRepository; _databaseCreator = (IRelationalDatabaseCreator)databaseCreator; _migrationsSqlGenerator = migrationsSqlGenerator; _rawSqlCommandBuilder = rawSqlCommandBuilder; _migrationCommandExecutor = migrationCommandExecutor; _connection = connection; _sqlGenerationHelper = sqlGenerationHelper; _currentContext = currentContext; _conventionSetBuilder = conventionSetBuilder; _logger = logger; _commandLogger = commandLogger; _activeProvider = databaseProvider.Name; }
/// <summary> /// Executes pending migrations and update procedures. /// </summary> public void ApplayMigrations() { _logger.LogInformation("===== Release Management ====="); try { _logger.LogInformation($"Hosting env: {_hostingEnvironment}"); IRelationalDatabaseCreator relationalDatabaseCreator = _context.Database.GetService <IDatabaseCreator>() as IRelationalDatabaseCreator; if (!relationalDatabaseCreator.Exists()) { _logger.LogInformation($"Creating new Database"); relationalDatabaseCreator.Create(); _logger.LogInformation($"New Database was created"); } IEnumerable <IUpdate> updates = _updateList.Get(); List <string> applyedMigrations = _context.Database .GetAppliedMigrations() .ToList(); foreach (var update in updates) { if (update.ShouldExecute(applyedMigrations)) { PerformUpdateInTransaction(update); } } _logger.LogInformation("===== Release Management Finished ====="); } catch (Exception ex) { _logger.LogError($"Error during ReleaseManagment. {ex}"); throw new Exception($"ReleaseManagment Error. {ex.Message}"); } }
private async Task CreateDataBase(IRelationalDatabaseCreator databaseCreator, DatabaseFacade dataBase) { //创建数据库 await databaseCreator.CreateAsync(); var sqlScript = dataBase.GenerateCreateScript(); //创建数据表 var sqlCommands = GetCommandsFromScript(sqlScript); foreach (var command in sqlCommands) { await dataBase.ExecuteSqlRawAsync(command); } //创建视图 ExecuteSqlScriptFromFile(dataBase, $"{SqlServerScriptPath}\\SqlServer_Views.sql"); //创建存储过程 ExecuteSqlScriptFromFile(dataBase, $"{SqlServerScriptPath}\\SqlServer_StoredProcedures.sql"); //执行存储过程 ExecuteProcedure(dataBase, "Update_DataBase @dataBase = @p0", _dataBaseName); //创建自定义脚本 ExecuteSqlScriptFromFile(dataBase, $"{SqlServerScriptPath}\\SqlServer_Custom.sql"); //初始化数据脚本 ExecuteSqlScriptFromFile(dataBase, $"{SqlServerScriptPath}\\SqlServer_Data.sql"); }
/// <summary> /// Executes pending migrations and update procedures. /// </summary> public static void ApplyIdentityMigrations(this IApplicationBuilder app) { using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope()) { ILoggerFactory loggerFactory = serviceScope.ServiceProvider.GetService <ILoggerFactory>(); ILogger logger = loggerFactory.CreateLogger(typeof(ReleaseManagement)); IOptionsSnapshot <DatabaseOptions> databaseOptions = serviceScope.ServiceProvider.GetRequiredService <IOptionsSnapshot <DatabaseOptions> >(); if (databaseOptions.Value.Type == DatabaseTypes.InMemory) { return; } logger.LogInformation("===== Release Management ====="); try { #if NET_CORE2 IHostingEnvironment hostingEnvironment = serviceScope.ServiceProvider.GetRequiredService <IHostingEnvironment>(); #endif #if NET_CORE3 IWebHostEnvironment hostingEnvironment = serviceScope.ServiceProvider.GetRequiredService <IWebHostEnvironment>(); #endif IConfiguration configuration = serviceScope.ServiceProvider.GetRequiredService <IConfiguration>(); IdentityDbContext context = serviceScope.ServiceProvider.GetRequiredService <IdentityDbContext>(); IRelationalDatabaseCreator databaseCreator = context.Database.GetService <IDatabaseCreator>() as IRelationalDatabaseCreator; logger.LogInformation($"Hosting env: {hostingEnvironment}"); if (!databaseCreator.Exists()) { logger.LogInformation($"Creating new Database"); databaseCreator.Create(); logger.LogInformation($"New Database was created"); } List <IUpdate> updates = AllUpdates(); logger.LogInformation($"All updates(also already applied): {string.Join(", ", updates)}"); List <string> applyedMigrations = context.Database .GetAppliedMigrations() .ToList(); foreach (var update in updates) { if (update.ShouldExecute(applyedMigrations)) { PerformUpdateInTransaction(context, update, logger); } } logger.LogInformation("===== Release Management Finished ====="); } catch (Exception ex) { logger.LogError($"Error during ReleaseManagment. {ex}"); throw new Exception($"ReleaseManagment Error. {ex.Message}"); } } return; }