Is there a way to run EF Core RC2 tools from published DLL?

asp.net-core entity-framework-core

Question

After publishing a .Net Core RC1 application, commands specified in the project.json had corresponding .cmd files created for them which could be executed after deployment (e.g. web.cmd and ef.cmd). In my case, I want to run the following Entity Framework command on my deployment target:

dotnet ef database update -c MyContext

This works fine when I run this from the folder containing the source code, however after publishing, it doesn't appear to find the command within the compiled DLLs. My understanding of the change in commands with RC2 is that 'tools' can be compiled as standalone applications named dotnet-*.dll and can be executed via the CLI. How can the Entity Framework Core tools be exposed as executable DLLs in the published output?

FYI, my build/deployment workflow is as follows:

TeamCity

dotnet restore => dotnet build => dotnet test => dotnet publish

Octopus Deploy

Upload Package => EF Update Database => etc

1
18
6/1/2016 7:28:01 AM

Accepted Answer

I ended up in the same problem on a project but for several reasons I don't want migrations to run automatically on application boot.

To solve it I updated Program.cs to take two arguments (full code is listed below)

  • --ef-migrate, to apply all pending migrations, and
  • --ef-migrate-check, to validate if all migrations have been applied

If arguments are present then the EF actions are applied and the program exits, otherwise the web application is launched.

Please note that it depends on the Microsoft.Extensions.CommandLineUtils package to ease the command line parsing.

For octopus deploy one can then publish the package twice to seperate locations - one for running migrations and the other for webhosting. In our case, we added a "post deploy powershell script" with the content

$env:ASPNETCORE_ENVIRONMENT="#{Octopus.Environment.Name}"
dotnet example-app.dll --ef-migrate

In a docker context it would work perfectly too

docker run -it "example-app-container" dotnet example-app.dll --ef-migrate

Full Program.cs excluding namespace and usings:

//Remember to run: dotnet add package Microsoft.Extensions.CommandLineUtils
public class Program
{
    public static void Main(string[] args)
    {
        var commandLineApplication = new CommandLineApplication(false);
        var doMigrate = commandLineApplication.Option(
            "--ef-migrate",
            "Apply entity framework migrations and exit",
            CommandOptionType.NoValue);
        var verifyMigrate = commandLineApplication.Option(
            "--ef-migrate-check",
            "Check the status of entity framework migrations",
            CommandOptionType.NoValue);
        commandLineApplication.HelpOption("-? | -h | --help");
        commandLineApplication.OnExecute(() =>
        {
            ExecuteApp(args, doMigrate, verifyMigrate);
            return 0;
        });
        commandLineApplication.Execute(args);
    }

    private static void ExecuteApp(string[] args, CommandOption doMigrate, CommandOption verifyMigrate)
    {
        Console.WriteLine("Loading web host");
        var webHost = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        if (verifyMigrate.HasValue() && doMigrate.HasValue())
        {
            Console.WriteLine("ef-migrate and ef-migrate-check are mutually exclusive, select one, and try again");
            Environment.Exit(2);
        }

        if (verifyMigrate.HasValue())
        {
            Console.WriteLine("Validating status of Entity Framework migrations");
            using (var context = webHost.Services.GetService<DatabaseContext>())
            {
                var pendingMigrations = context.Database.GetPendingMigrations();
                var migrations = pendingMigrations as IList<string> ?? pendingMigrations.ToList();
                if (!migrations.Any())
                {
                    Console.WriteLine("No pending migratons");
                    Environment.Exit(0);
                }

                Console.WriteLine("Pending migratons {0}", migrations.Count());
                foreach (var migration in migrations)
                {
                    Console.WriteLine($"\t{migration}");
                }

                Environment.Exit(3);
            }
        }

        if (doMigrate.HasValue())
        {
            Console.WriteLine("Applyting Entity Framework migrations");
            using (var context = webHost.Services.GetService<DatabaseContext>())
            {
                context.Database.Migrate();
                Console.WriteLine("All done, closing app");
                Environment.Exit(0);
            }
        }

        // no flags provided, so just run the webhost
        webHost.Run();
    }
}
14
4/25/2017 2:51:08 PM

Popular Answer

Unfortunately EF Core migration s*cks, a lot... I have seen tons of solutions for this but lets do a list of them. So here is what you can do to run and deploy EF migrations without Visual Studio. None of the below is perfect solution all have some caveats:

  1. Use EF Core Tools here is a link to the official MS site which explains how to install and use it.
    • Pros: MS official tool. Supported in all version of .NET Core.
    • Cons: It seems like the successor of EF6 "Migrate.exe". But it is not! Currently it is not possible to use this tool without the actual source code (.csproj). Which is not really a good fit for Live/Prod deployments. Usually you don't have C# projects on your DB Server.
  2. dotnet exec I have tried to make sense of the huge amount of poorly documented parameters. And failed to run migrations until found this script. The name suggest .NET core 2.1 but I have used it with 3.0 and worked.
    • Pros: It can be used like EF6 "migrate.exe". Finally migration works without the source code. And most probably this is the only way to do migration from script and using an Assembly.
    • Cons: Very hard to set up the script, and easy to miss one parameter. Not really documented solution and might change .NET Core version to version. Also most probably you will need to change your code as well. You have to implement IDesignTimeDbContextFactory<DbContext> interface in order to make it work. Also make sure you have EF.dll and Microsoft.EntityFrameworkCore.Design.dll on your deploy server. The linked script is looking for those in numerous folders. Best is to copy it during build from your .nuget folders to your artifact. Sounds complicated, yes it is... But linked script helps a lot.
  3. Add EF migration to your Startup.cs or any point where your code start running and has access to a DBContext. Use dbContext.Database.Migrate();
    • Pros: Migrations happens automatically every time and nothing else had to be done.
    • Cons: Migrations happens automatically every time... The problem you might don't want to that happen. Also it will run on every App start. So your startup time will be very bad.
  4. Custom app. It is similar to the previous solution (point 3.). So you use .NET code to run migration. But instead of putting it into your app you should create a small console app and call migrate in that one. You have to build this app and put into the Artifact to run it during the deployment.
    • Pros: No script involved. You can call it any time in your deployment pipeline. So your real app startup time not suffers from it.
    • Cons: You have to maintain, build and package an application just to do EF Core migrations.
  5. If you are using Azure Devops for deploy, you can use extension like this. Or just search Azure Devops Marketplace for one.
    • Pros: it should work :) Haven't tried any of them and don't know what they do. (I'm pretty sure they are also using 'dotnet exec' point 2.)
    • Cons: Not everyone can have access to Live/Prod from Azure Devops.

Sorry the list got very long. But hope it helps.



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