无法在注册多个 DbContext 的项目中添加 EF Core 迁移

Cannot add EF Core migrations in project that registers multiple DbContext

我有一个控制台应用程序,它有两个 DbContext。一个是使用 MassTransit 配置的,另一个是对我已经 运行 迁移的另一个项目的引用。因此,我想 运行 为 MassTransit 配置的 DbContext 迁移此服务,我收到以下错误消息:

“找到多个 DbContext。指定要使用的一个。对 PowerShell 命令使用“-Context”参数,对 dotnet 命令使用“--context”参数。“

但是,当我使用“dotnet ef migrations add InitialCreate -c CourierServiceDbContext”指定上下文时,出现以下错误:

“无法创建 'CourierServiceDbContext' 类型的对象。有关设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728"

明确地说,我只想运行 迁移 MassTransit 配置的 DbContext、CourierServiceDbContext 而不是引用的 DbContext、OrdersDbContext。

static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder()
        .UseSerilog((host, log) =>
        {
            string? appBin = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            log.MinimumLevel.Information();
            log.WriteTo.File($"{appBin}/log/log-{DateTime.Now:yyMMdd_HHmmss}.txt");
            log.WriteTo.Console(LogEventLevel.Debug);
        })
        .ConfigureAppConfiguration((host, builder) =>
        {
            builder.AddJsonFile("appsettings.json", false);
        })
        .ConfigureServices((host, services) =>
        {
            services.AddDbContext<OrdersDbContext>(x =>
                x.UseNpgsql(host.Configuration.GetConnectionString("OrdersConnection")));
            
            services.AddMassTransit(x =>
            {
                x.AddConsumer<CourierDispatchConsumer>();
                
                x.SetKebabCaseEndpointNameFormatter();
                
                x.UsingRabbitMq((context, cfg) =>
                {
                    string vhost = host.Configuration
                        .GetSection("Application")
                        .GetValue<string>("VirtualHost");
                    
                    cfg.Host("localhost", vhost, h =>
                    {
                        h.Username("guest");
                        h.Password("guest");
                    });
                    
                    cfg.ConfigureEndpoints(context);
                    // cfg.UseMessageRetry(x => x.SetRetryPolicy(new RetryPolicyFactory()));
                });

                x.AddSagaStateMachine<CourierStateMachine, CourierState>()
                    .EntityFrameworkRepository(r =>
                    {
                        r.ConcurrencyMode = ConcurrencyMode.Optimistic;
                        
                        r.AddDbContext<DbContext, CourierServiceDbContext>((provider, builder) =>
                        {
                            builder.UseNpgsql(host.Configuration.GetConnectionString("OrdersConnection"), m =>
                            {
                                m.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().Name);
                                m.MigrationsHistoryTable($"__{nameof(CourierServiceDbContext)}");
                            });
                        });
                    });
            });

            services.AddMassTransitHostedService();
        });

通过调试各种错误,我发现了一些我必须做的事情:

  1. 向两个 DbContext 添加一个构造函数,它采用 DbContextOptions 而不是仅仅具有 DbContextOptions
  2. 使用上下文切换执行迁移创建,如“dotnet ef migrations add InitialCreate -c
  3. 使用上下文切换执行数据库同步命令,如“dotnet ef database update -c

此外,您的 class 依赖于 DbContext 应注册为作用域,否则上述步骤将不起作用。

这将允许您在同一服务中注册多个 DbContext。