0

I'm trying to add a migration via Add-Migration {Name} or dotnet ef migrations add {Name} with a .NET 6 Worker Service. It build successfully and I can see from the migration logs its trying to but just dies silently at

Using application service provider from Microsoft.Extensions.Hosting.

Here's the full stack log once it finishes building

dotnet exec --depsfile C:\Users\me\source\repos\SmartSafeFinancials\src\StoresDaysheetProcessor.Worker\bin\Debug\net6.0\StoresDaysheetProcessor.Worker.deps.json --additionalprobingpath C:\Users\me\.nuget\packages --runtimeconfig C:\Users\me\source\repos\SmartSafeFinancials\src\StoresDaysheetProcessor.Worker\bin\Debug\net6.0\StoresDaysheetProcessor.Worker.runtimeconfig.json C:\Users\me\.dotnet\tools\.store\dotnet-ef\6.0.2\dotnet-ef\6.0.2\tools\netcoreapp3.1\any\tools\netcoreapp2.0\any\ef.dll migrations add Initialcreate -o ./Data/SmartSafeFinancials/Migrations --context SmartSafeFinancialsDbContext --assembly C:\Users\me\source\repos\SmartSafeFinancials\src\StoresDaysheetProcessor.Worker\bin\Debug\net6.0\StoresDaysheetProcessor.Worker.dll --project C:\Users\me\source\repos\SmartSafeFinancials\src\StoresDaysheetProcessor.Worker\StoresDaysheetProcessor.Worker.csproj --startup-assembly C:\Users\me\source\repos\SmartSafeFinancials\src\StoresDaysheetProcessor.Worker\bin\Debug\net6.0\StoresDaysheetProcessor.Worker.dll --startup-project C:\Users\me\source\repos\SmartSafeFinancials\src\StoresDaysheetProcessor.Worker\StoresDaysheetProcessor.Worker.csproj --project-dir C:\Users\me\source\repos\SmartSafeFinancials\src\StoresDaysheetProcessor.Worker\ --root-namespace StoresDaysheetProcessor.Worker --language C# --framework net6.0 --nullable --working-dir C:\Users\me\source\repos\SmartSafeFinancials\src\StoresDaysheetProcessor.Worker --verbose
Using assembly 'StoresDaysheetProcessor.Worker'.
Using startup assembly 'StoresDaysheetProcessor.Worker'.
Using application base 'C:\Users\me\source\repos\SmartSafeFinancials\src\StoresDaysheetProcessor.Worker\bin\Debug\net6.0'.
Using working directory 'C:\Users\me\source\repos\SmartSafeFinancials\src\StoresDaysheetProcessor.Worker'.
Using root namespace 'StoresDaysheetProcessor.Worker'.
Using project directory 'C:\Users\me\source\repos\SmartSafeFinancials\src\StoresDaysheetProcessor.Worker\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'StoresDaysheetProcessor.Worker'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.

Database context

public class SmartSafeFinancialsDbContext : DbContext
{
    public SmartSafeFinancialsDbContext(DbContextOptions<SmartSafeFinancialsDbContext> options) : base(options)
    {
    }

    public DbSet<StoreDaysheetDay> StoreDaysheetDays { get; set; } = null!;

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
}

Program.cs

    var builder = Host.CreateDefaultBuilder(args)
        .ConfigureServices((builder, services) =>
        {
            services.AddDbContextFactory<SmartSafeFinancialsDbContext>(options =>
            {
             
   options.UseSqlServer(_configuration.GetConnectionString("SmartSafeFinancials"));
            });
        });

    var host = builder.Build();
    await host.RunAsync();
Ben Sampica
  • 2,912
  • 1
  • 20
  • 25
  • See following : https://stackoverflow.com/questions/60561851/an-error-occurred-while-accessing-the-microsoft-extensions-hosting-services-when?force_isolation=true – jdweng Feb 16 '22 at 15:13
  • I saw this, but unfortunately it didn't work by just adding a `IDesignTimeDbContextFactory` directly to my worker service. Confusingly, I'm getting no errors unlike that OP. – Ben Sampica Feb 16 '22 at 15:23
  • Did it complete? Did you try running code? Did you check Event Veiwer for errors? – jdweng Feb 16 '22 at 15:35
  • No - as stated in the question it gets to `Microsoft.Extensions.Hosting` and exits silently. No Event Viewer logs. Not sure what you mean by "try running code". The code works as far as production code is concerned. – Ben Sampica Feb 16 '22 at 15:44
  • Maybe it exit because it finished successfully. – jdweng Feb 16 '22 at 15:57
  • Unfortuantely, it's not. A normal migration log acquires a DbContext and generates a migration - which is all displayed in the verbose logging. – Ben Sampica Feb 16 '22 at 16:24
  • Did you see this link on above page : https://learn.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli&force_isolation=true – jdweng Feb 16 '22 at 16:33
  • As stated in my first comment, adding a IDesignTimeDbContextFactory to my worker service for my DbContext invokes _no change to this non-working behavior_. – Ben Sampica Feb 16 '22 at 17:52
  • Is the connection completing? What changed between working and non working conditions beside the Net version? Did you check log files in database for errors? Did you download latest runtime version of Core on deploy machine (or are you running on same machine as build)? – jdweng Feb 16 '22 at 18:09
  • This is to generate a migration. What does the database connection have to do with the differences between the Modelsnapshot and the model itself? I’m not trying to apply a migration I’m trying to add one to be applied later. – Ben Sampica Feb 16 '22 at 22:44
  • You are using Entity where there is a mapping file that associates the DataBase Tables to the c# classes which needs updating and you need to connect to the server to be able to do the updating. – jdweng Feb 17 '22 at 09:48
  • Yes, but I don’t need the database connection until after this step. Separately, and at a later date. There is no database connection to generate a migration. – Ben Sampica Feb 17 '22 at 13:29
  • The migration isn't completing probably because the connection cannot be made. You can't have your cake and pie too! – jdweng Feb 17 '22 at 16:35
  • Thanks for trying to help. – Ben Sampica Feb 17 '22 at 17:05

1 Answers1

0

I ended up having to create a separate project and then using the normal IDesignTimeDbContextFactory<>. Not ideal since I didn't want the bloat of a second project but that allowed me to get through the command line to add migrations to later be applied.

Ben Sampica
  • 2,912
  • 1
  • 20
  • 25