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?
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.
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)