4

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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.