简体   繁体   English

如何在 .net 的 .NET 工作者服务中生成迁移 6

[英]How to generate migrations in a .NET worker service for .net 6

I'm trying to add a migration via Add-Migration {Name} or do.net ef migrations add {Name} with a .NET 6 Worker Service.我正在尝试通过Add-Migration {Name}do.net ef migrations add {Name}一个带有 .NET 6 Worker 服务的迁移。 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.使用来自 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程序.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();

I ended up having to create a separate project and then using the normal IDesignTimeDbContextFactory<> .我最终不得不创建一个单独的项目,然后使用普通的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.这并不理想,因为我不想让第二个项目膨胀,但这让我可以通过命令行添加迁移以供以后应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM