コード例 #1
0
        // Internal for testing
        protected internal virtual string GetNamespace(
            [CanBeNull] ModelSnapshot modelSnapshot,
            [NotNull] string migrationNamespace)
        {
            Check.NotEmpty(migrationNamespace, "migrationNamespace");

            return(modelSnapshot?.GetType().Namespace ?? migrationNamespace);
        }
コード例 #2
0
        protected virtual void DiffSnapshot(ModelSnapshot snapshot, DbContext context)
        {
            var sourceModel = snapshot.Model;
            var targetModel = context.Model;

            var modelDiffer = context.GetService <IMigrationsModelDiffer>();
            var operations  = modelDiffer.GetDifferences(sourceModel, targetModel);

            Assert.Equal(0, operations.Count);
        }
コード例 #3
0
        public async Task <MigrationScriptExecutor> PrepareMigration()
        {
            migrationScriptExecutor = new MigrationScriptExecutor(dbMigratorProps);

            var migrationAssembly = dbMigratorProps.dbContext.GetService <IMigrationsAssembly>();

            var declaredMigrations = dbMigratorProps.dbContext.Database.GetMigrations().ToList();
            var appliedMigrations  = (await dbMigratorProps.dbContext.Database.GetAppliedMigrationsAsync()).ToList();

            if (declaredMigrations.Except(appliedMigrations).Any())
            {
                throw new InvalidOperationException("Pending migration scripts have been found. Please run those migrations first () before trying to use the DBMigrator.");
            }

            string dbMigratorRunMigrations = null;
            var    lastRunMigration        = appliedMigrations.LastOrDefault();

            if (lastRunMigration != null && declaredMigrations.Find(s => string.Compare(s, lastRunMigration) == 0) == null)
            {
                dbMigratorRunMigrations = lastRunMigration;
            }

            migrationScriptExecutor.EnsureMigrateTablesExist();

            ModelSnapshot modelSnapshot = null;

            if (dbMigratorRunMigrations != null)
            {
                AutoMigratorTable migrationRecord = migrationScriptExecutor.GetLastMigrationRecord();
                var source = await Utilities.Utilities.DecompressSource(migrationRecord.snapshot);

                if (source == null || !source.Contains(dbMigratorRunMigrations))
                {
                    throw new InvalidOperationException($"Expected to find the source code of the {dbMigratorRunMigrations} ModelSnapshot stored in the database");
                }

                try{
                    modelSnapshot = Utilities.Utilities.CompileSnapshot(migrationAssembly.Assembly, dbMigratorProps.dbContext, source);
                }
                catch (Exception ex) {
                    throw new InvalidOperationException("Failed to compile previous snapshot. This usually occurs when you have changed the namespace(s) associates with your DBSets. To fix you will have to delete the table causing the problem in your database (see below).", ex);
                }
            }
            else
            {
                modelSnapshot = migrationAssembly.ModelSnapshot;
            }

            if (SetMigrationCommands(migrationAssembly.Assembly, modelSnapshot?.Model, dbMigratorProps.dbContext.Model))
            {
                await UpdateMigrationTables();
            }

            return(migrationScriptExecutor);
        }
コード例 #4
0
        protected virtual void DiffSnapshot(ModelSnapshot snapshot, DbContext context)
        {
            var sourceModel = context.GetService <IModelRuntimeInitializer>().Initialize(
                ((IMutableModel)snapshot.Model).FinalizeModel(), designTime: true, validationLogger: null);

            var modelDiffer = context.GetService <IMigrationsModelDiffer>();
            var operations  = modelDiffer.GetDifferences(
                sourceModel.GetRelationalModel(),
                context.DesignTimeModel.GetRelationalModel());

            Assert.Equal(0, operations.Count);
        }
コード例 #5
0
        protected virtual void DiffSnapshot(ModelSnapshot snapshot, DbContext context)
        {
            var dependencies          = context.GetService <ProviderConventionSetBuilderDependencies>();
            var typeMappingConvention = new TypeMappingConvention(dependencies);

            typeMappingConvention.ProcessModelFinalizing(((IConventionModel)snapshot.Model).Builder, null);

            var relationalModelConvention = new RelationalModelConvention();
            var sourceModel = relationalModelConvention.ProcessModelFinalized(snapshot.Model);

            var modelDiffer = context.GetService <IMigrationsModelDiffer>();
            var operations  = modelDiffer.GetDifferences(((IMutableModel)sourceModel).FinalizeModel(), context.Model);

            Assert.Equal(0, operations.Count);
        }
コード例 #6
0
        protected virtual void DiffSnapshot(ModelSnapshot snapshot, DbContext context)
        {
            var sourceModel = snapshot.Model;
            var targetModel = context.Model;

            var typeMapper = context.GetService <IRelationalTypeMappingSource>();

            foreach (var property in sourceModel.GetEntityTypes().SelectMany(e => e.GetDeclaredProperties()))
            {
                Assert.NotNull(typeMapper.FindMapping(property));
            }

            foreach (var property in targetModel.GetEntityTypes().SelectMany(e => e.GetDeclaredProperties()))
            {
                Assert.NotNull(typeMapper.FindMapping(property));
            }

            var modelDiffer = context.GetService <IMigrationsModelDiffer>();
            var operations  = modelDiffer.GetDifferences(sourceModel, targetModel);

            Assert.Equal(0, operations.Count);
        }