question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MigrationStage.AfterAll doesn't get executed when registering migrations via .ScanIn(typeof(Program).Assembly).For.Migrations()

See original GitHub issue

When trying to run a Maintainance script on sql server it doesn’t get executed. I’ve tried to step into the source and it seems like AssemblySourceOptions doesn’t return any assemblies. My entire source is here. I use .net core. What is the problem. I cannot find documentation on this.

My goal is to have all stored proc applied on the database on every single Migrate run - via the class Id_999999999999_RunAllStoredProcedures.

class Program
    {
        static void Main(string[] args)
        {
            IServiceCollection services = new ServiceCollection();
            const string connectionString = @"Server=.\SQLEXPRESS;Database=Cv;Trusted_Connection=true";
            services
                .AddFluentMigratorCore()
                .ConfigureRunner(
                    x => x.AddSqlServer2016()
                        .WithGlobalConnectionString(connectionString)
                        .ScanIn(typeof(Program).Assembly).For.Migrations());
            var provider = services.BuildServiceProvider();

            using (IServiceScope scope = provider.CreateScope())
            {
                IMigrationRunner runner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>();
                runner.MigrateUp();
            }
        }
    }

    [Migration(1)]
    public class TestMigration : Migration
    {
        /// <inheritdoc />
        public override void Up()
        {
            Create.Table("test").WithColumn("Id").AsString();
        }

        /// <inheritdoc />
        public override void Down()
        {
            throw new NotImplementedException();
        }
    }

    [Maintenance(MigrationStage.AfterAll)]
    public class Id_999999999999_RunAllStoredProcedures : Migration
    {
        public override void Up()
        {
            Execute.Sql("CREATE OR ALTER PROC a AS BEGIN SELECT 1 END");
        }

        public override void Down()
        {
            //NOOP
        }
    }

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:20 (1 by maintainers)

github_iconTop GitHub Comments

0reactions
habprogcommented, Mar 6, 2023

Hi, I would like to add that issue still exists. I start today work with FluentMigrator and I spent 3 hours to find this ticket, which has a workaround. It would be good to update documentation how to write In-Process application with working MaintenaceAttribute. https://fluentmigrator.github.io/articles/migration-example.html https://fluentmigrator.github.io/articles/glossary.html

In summary: workaround which adds IAssemblySourceItem (I don’t know what it is, I just start play with FluentMigrator) which suggests @jzabroski works.

I also found another workaround without adding IAssemblySourceItem: Changing .ScanIn(typeof(Program).Assembly).For.Migrations() to .ScanIn(typeof(Program).Assembly).For.All() also works.

I tried also combination, but this doesn’t work.

.ScanIn(typeof(Program).Assembly)
    .For.Migrations()
    .For.EmbeddedResources()
    .For.VersionTableMetaData()

After following this process, the error still persists what change is I targeted my migration file to be in my .net core API project directory, not the class library project. “It works for me”

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add migration with different assembly
Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing " ...
Read more >
Using a Separate Migrations Project - EF Core
Using a separate migration project for managing database schemas with Entity Framework Core.
Read more >
Fix: Your target project doesn't match your migration assembly
In this video, I am going to show you , How to fix that error " Your target project "Projectname" doesn't match your...
Read more >
Migrations and Seed Data With Entity Framework Core
We are going to learn about Migrations and Seed data in Entity Framework ... EF Core provides a method called Migrate to execute...
Read more >
Applying Migrations - EF Core
Strategies for applying schema migrations to production and development databases using Entity Framework Core.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found