/// <summary>
        /// Initializes the database.
        /// </summary>
        /// <param name="context">The context.</param>
        public virtual void InitializeDatabase(TContext context)
        {
            if (!context.Database.Exists())
            {
                throw Error.InvalidOperation("Database migration failed because the target database does not exist. Ensure the database was initialized and seeded with the 'InstallDatabaseInitializer'.");
            }

            var config = CreateConfiguration();

            var migrator    = new DbSeedingMigrator <TContext>(config);
            var tablesExist = CheckTables(context);

            if (tablesExist)
            {
                // Tables specific to the model DO exist in the database...
                var hasHistoryEntry = migrator.GetDatabaseMigrations().Any();
                if (!hasHistoryEntry)
                {
                    // ...but there is no entry in the __MigrationHistory table (or __MigrationHistory doesn't exist at all)
                    // We MUST assume that the database was created with a previous SmartStore version
                    // prior integrating EF Migrations.
                    // Running the Migrator with initial DDL would crash in this case as
                    // the db objects exist already. Therefore we set a suppression flag
                    // which we read in the corresponding InitialMigration to exit early.
                    DbMigrationContext.Current.SetSuppressInitialCreate <TContext>(true);
                }
            }

            using (new DbContextScope(context as IDbContext, hooksEnabled: false))
            {
                // run all pending migrations
                var appliedCount = migrator.RunPendingMigrations(context);

                if (appliedCount > 0)
                {
                    Seed(context);
                }
                else
                {
                    // DB is up-to-date and no migration ran.
                    EfMappingViewCacheFactory.SetContext(context);

                    if (config is MigrationsConfiguration coreConfig && context is SmartObjectContext ctx)
                    {
                        // Call the main Seed method anyway (on every startup),
                        // we could have locale resources or settings to add/update.
                        coreConfig.SeedDatabase(ctx);
                    }
                }

                // not needed anymore
                this.DataSeeders = null;
            }
        }
示例#2
0
        /// <summary>
        /// Initializes the database.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <inheritdoc />
        public virtual void InitializeDatabase(TContext context)
        {
            if (!context.Database.Exists())
            {
                throw Error.InvalidOperation("Database migration failed becuase the target database does not exist. Ensure the database was initialized and seeded with the 'InstallDatabaseInitializer'.");
            }

            var config      = CreateConfiguration();
            var migrator    = new DbSeedingMigrator <TContext>(config);
            var tablesExist = CheckTables(context);

            if (tablesExist)
            {
                // Tables specific to the model DO exist in the database...
                var hasHistoryEntry = migrator.GetDatabaseMigrations().Any();
                if (!hasHistoryEntry)
                {
                    // ...but there is no entry in the __MigrationHistory table (or __MigrationHistory doesn't exist at all)
                    // We MUST assume that the database was created with a previous SmartStore version
                    // prior integrating EF Migrations.
                    // Running the Migrator with initial DDL would crash in this case as
                    // the db objects exist already. Therefore we set a suppression flag
                    // which we read in the corresponding InitialMigration to exit early.
                    DbMigrationContext.Current.SetSuppressInitialCreate <TContext>(true);
                }
            }

            // run all pending migrations
            var appliedCount = migrator.RunPendingMigrations(context);

            if (appliedCount > 0)
            {
                Seed(context);
            }

            // not needed anymore
            this.DataSeeders = null;
        }