Including Entity Framework tooling in docker container

.net-core asp.net-core c# entity-framework entity-framework-core

Question

I am using the aspnetcore docker image to run an ASP.NET Core application using EF Core. Running the application works, but I am unable to use any tooling.

Commands like dotnet ef database update fail.

Is it possible to run any DotNetCliToolReference tooling without the full SDK?

1
4
2/12/2018 5:40:09 PM

Accepted Answer

Is it possible to run any DotNetCliToolReference tooling without the full SDK?

No. The logic to execute these tools lives in the SDK.

But, if you run this with --verbose on a machine with the SDK you'll see a line that looks like this:

dotnet exec ... ef.dll ...

You can run that command inside the container.

5
2/12/2018 5:52:11 PM

Popular Answer

You cannot use DotNetCliToolReference on a production.

But you can do it in two ways.

By resolving DbContext service and calling Database.Migrate()

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>() 
                    .CreateScope()) 
        { 
            serviceScope.ServiceProvider.GetService<DatabaseApplicationContext>() 
                .Database.Migrate(); 
        } 

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }

Or directly by calling Database.Migrate() in DbContext class

    protected DatabaseApplicationContext()
    {
        Database.Migrate();            
    }

    public DatabaseApplicationContext(DbContextOptions options) : base(options)
    {
        Database.Migrate();
    }

I would recommend first option because migration is done only at application startup. But the second one will be called when DbContext is created (most likely at every request, if you keep default DbContext scope - as a Scoped service)



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