Search code examples
c#entity-framework-coremigration

Deploy .NET 7 class library project with dotnet ef migration


I have a .NET 7 solution with one project being a npgsql layer class library. I need to deploy it with dotnet ef migration tool, but without the .NET Core SDK, in an isolated network.

I'm trying to run this:

dotnet publish -c Release -r linux-x64 --self-contained true

but it returned an error that "Microsoft.NETCore.App.Runtime.linux-x64 cannot be found". When I try to install this nuget I'm getting incompatible error.

What is the way to install EF Core migration tool without the SDK?


Solution

  • It needs to run "dotnet ef database update" command

    I would recommend a different approach: instead of using tools - use the migrator infrastructure provided by the EF via the IMigrator interface (otherwise it is highly likely you will need the SDK in the pod):

    IServiceProvider _serviceProvider = ...;
    
    // ...
    
    // Get the context - AppCtx
    var requiredService = _serviceProvider.GetRequiredService<AppCtx>();
    
    // Resolve and use migrator:
    var migrator = requiredService.GetService<IMigrator>();
    await migrator.MigrateAsync(null, stoppingToken);