如何在.NET worker服务中为.NET 6生成迁移



我正试图通过带有.NET 6 Worker Service的Add-Migration {Name}dotnet ef migrations add {Name}添加迁移。它成功地构建了,我可以从迁移日志中看到它正在尝试,但在中只是默默地死去

使用Microsoft.Extensions.Hosting.中的应用程序服务提供商

这是构建后的完整堆栈日志

dotnet exec --depsfile C:UsersmesourcereposSmartSafeFinancialssrcStoresDaysheetProcessor.WorkerbinDebugnet6.0StoresDaysheetProcessor.Worker.deps.json --additionalprobingpath C:Usersme.nugetpackages --runtimeconfig C:UsersmesourcereposSmartSafeFinancialssrcStoresDaysheetProcessor.WorkerbinDebugnet6.0StoresDaysheetProcessor.Worker.runtimeconfig.json C:Usersme.dotnettools.storedotnet-ef6.0.2dotnet-ef6.0.2toolsnetcoreapp3.1anytoolsnetcoreapp2.0anyef.dll migrations add Initialcreate -o ./Data/SmartSafeFinancials/Migrations --context SmartSafeFinancialsDbContext --assembly C:UsersmesourcereposSmartSafeFinancialssrcStoresDaysheetProcessor.WorkerbinDebugnet6.0StoresDaysheetProcessor.Worker.dll --project C:UsersmesourcereposSmartSafeFinancialssrcStoresDaysheetProcessor.WorkerStoresDaysheetProcessor.Worker.csproj --startup-assembly C:UsersmesourcereposSmartSafeFinancialssrcStoresDaysheetProcessor.WorkerbinDebugnet6.0StoresDaysheetProcessor.Worker.dll --startup-project C:UsersmesourcereposSmartSafeFinancialssrcStoresDaysheetProcessor.WorkerStoresDaysheetProcessor.Worker.csproj --project-dir C:UsersmesourcereposSmartSafeFinancialssrcStoresDaysheetProcessor.Worker --root-namespace StoresDaysheetProcessor.Worker --language C# --framework net6.0 --nullable --working-dir C:UsersmesourcereposSmartSafeFinancialssrcStoresDaysheetProcessor.Worker --verbose
Using assembly 'StoresDaysheetProcessor.Worker'.
Using startup assembly 'StoresDaysheetProcessor.Worker'.
Using application base 'C:UsersmesourcereposSmartSafeFinancialssrcStoresDaysheetProcessor.WorkerbinDebugnet6.0'.
Using working directory 'C:UsersmesourcereposSmartSafeFinancialssrcStoresDaysheetProcessor.Worker'.
Using root namespace 'StoresDaysheetProcessor.Worker'.
Using project directory 'C:UsersmesourcereposSmartSafeFinancialssrcStoresDaysheetProcessor.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.

数据库上下文

public class SmartSafeFinancialsDbContext : DbContext
{
public SmartSafeFinancialsDbContext(DbContextOptions<SmartSafeFinancialsDbContext> options) : base(options)
{
}
public DbSet<StoreDaysheetDay> StoreDaysheetDays { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}

程序.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();

我最终不得不创建一个单独的项目,然后使用普通的IDesignTimeDbContextFactory<>。这并不理想,因为我不希望第二个项目出现膨胀,但这让我可以通过命令行添加稍后应用的迁移。

最新更新