Search code examples
c#asp.net-coreself-contained

Run migrations in self-contained applications


I have created a self-contained application with dotnet:

dotnet publish -c release

The deployment package contains .net core runtime itself - so if somebody wants to use app - doesn't need to install .net core runtimes separately.

But my question is... is it possible to attach dotnet.exe to deployment package?

F.e. - my web app expectes migrated database:

dotnet ef database update

This command cannot be run from self-contained application (no direct access to dotnet.exe).

What are scenarios in this case? How should I handle it?


Solution

  • The solution is to add it on your Startup.cs like that

    public void Configure(
        IApplicationBuilder app,
        // ... various other parameters that you may have and below add the context that you want to run Migrations
        YourDbContext db1)
    {
    {
        // run the migrations before other code
        db1.Database.Migrate();
        // ...
    }
    

    PS. it is a bit weird, but you add it on the Configure method signature as an extra parameter and it works (I guess it instantiates the dbContext object automatically and calls the method). It worked for me.