dotnet ef migrations add: create migration file only if there are changes

entity-framework-core entity-framework-core-migrations

Question

I'm using dotnet ef migrations add {MigrationName} in order to create a new migration.

In case there are no Entities/Context changes this creates a migration with empty Up(MigrationBuilder migrationBuilder) and Down(MigrationBuilder migrationBuilder) functions.

Is there a way for the migrations add command to skip the creation of 'empty' files?

Alternatively, Is there a way to detect if there are changes before running the migrations add command?

1
1
4/24/2018 9:33:26 PM

Accepted Answer

I found a solution to this problem.

There is a way to create migrations programmatically instead of using CLI.

After looking at the MigrationsScaffolder source code I managed to modify the code in the first link in order to support my needs:

        using (var db = new MyDbContext())
        {
            var reporter = new OperationReporter(
                new OperationReportHandler(
                    m => Console.WriteLine("  error: " + m),
                    m => Console.WriteLine("   warn: " + m),
                    m => Console.WriteLine("   info: " + m),
                    m => Console.WriteLine("verbose: " + m)));

            var designTimeServices = new ServiceCollection()
                .AddSingleton(db.GetService<IHistoryRepository>())
                .AddSingleton(db.GetService<IMigrationsIdGenerator>())
                .AddSingleton(db.GetService<IMigrationsModelDiffer>())
                .AddSingleton(db.GetService<IMigrationsAssembly>())
                .AddSingleton(db.Model)
                .AddSingleton(db.GetService<ICurrentDbContext>())
                .AddSingleton(db.GetService<IDatabaseProvider>())
                .AddSingleton<MigrationsCodeGeneratorDependencies>()
                .AddSingleton<ICSharpHelper, CSharpHelper>()
                .AddSingleton<CSharpMigrationOperationGeneratorDependencies>()
                .AddSingleton<ICSharpMigrationOperationGenerator, CSharpMigrationOperationGenerator>()
                .AddSingleton<CSharpSnapshotGeneratorDependencies>()
                .AddSingleton<ICSharpSnapshotGenerator, CSharpSnapshotGenerator>()
                .AddSingleton<CSharpMigrationsGeneratorDependencies>()
                .AddSingleton<IMigrationsCodeGenerator, CSharpMigrationsGenerator>()
                .AddSingleton<IOperationReporter>(reporter)
                .AddSingleton<MigrationsScaffolderDependencies>()
                .AddSingleton<ISnapshotModelProcessor, SnapshotModelProcessor>()
                .AddSingleton<MigrationsScaffolder>()
                .BuildServiceProvider();

            var scaffolderDependencies = designTimeServices.GetRequiredService<MigrationsScaffolderDependencies>();

            var modelSnapshot = scaffolderDependencies.MigrationsAssembly.ModelSnapshot;
            var lastModel = scaffolderDependencies.SnapshotModelProcessor.Process(modelSnapshot?.Model);
            var upOperations = scaffolderDependencies.MigrationsModelDiffer.GetDifferences(lastModel, scaffolderDependencies.Model);
            var downOperations = upOperations.Any() ? scaffolderDependencies.MigrationsModelDiffer.GetDifferences(scaffolderDependencies.Model, lastModel) : new List<MigrationOperation>();

            if (upOperations.Count() > 0 || downOperations.Count() > 0)
            {
                var scaffolder = designTimeServices.GetRequiredService<MigrationsScaffolder>();

                var migration = scaffolder.ScaffoldMigration(
                    "MyMigration",
                    "MyApp.Data");

                File.WriteAllText(
                    migration.MigrationId + migration.FileExtension,
                    migration.MigrationCode);
                File.WriteAllText(
                    migration.MigrationId + ".Designer" + migration.FileExtension,
                    migration.MetadataCode);
                File.WriteAllText(migration.SnapshotName + migration.FileExtension,
                   migration.SnapshotCode);
            }
        }
1
4/26/2018 9:17:44 PM

Popular Answer

I don't know if its possible at all, But empty migrations are helpful also like if you want to run any script manually, for example, create/alter StoredProcedures/Trigger. For this purpose, you can generate an empty migration exclusively for a particular purpose.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow