我使用干净的拱形步骤来创建项目,但问题是我需要将三个以上的聚合放在参考数据库中。
我尝试对每个聚合使用 DbContext,如下所示:
public class AppDbContext : DbContext
{
private readonly IMediator? _mediator;
public AppDbContext(DbContextOptions<AppDbContext> options, IMediator? mediator)
: base(options)
{
_mediator = mediator;
}
.
.
.
public class AnalyzeDbContext : DbContext
{
private readonly IMediator? _mediator;
public AnalyzeDbContext(DbContextOptions<AnalyzeDbContext> options, IMediator? mediator)
: base(options)
{
_mediator = mediator;
}
.
.
.
public class ProviderMgrDbContext : DbContext
{
private readonly IMediator? _mediator;
public ProviderMgrDbContext(DbContextOptions<ProviderMgrDbContext> options, IMediator? mediator)
: base(options)
{
_mediator = mediator;
}
我像这样发送每个 DbContext 的连接字符串:
//#Update 1
public static void AppDbContext(this IServiceCollection services, string connectionString) =>
services.AddDbContext<AppDbContext>(options => options.UseMySQL(connectionString));
public static void ProviderMgrDbContext(this IServiceCollection services, string connectionString) =>
services.AddDbContext<ProviderMgrDbContext>(options => options.UseMySQL(connectionString));
public static void AnalyzeDbContext(this IServiceCollection services, string connectionString) =>
services.AddDbContext<AnalyzeDbContext>(options => options.UseMySQL(connectionString));
//#After
public static void AppDbContext(this IServiceCollection services, string connectionString) =>
services.AddDbContext<AppDbContext>(options => options.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 27))));
public static void ProviderMgrDbContext(this IServiceCollection services, string connectionString) =>
services.AddDbContext<ProviderMgrDbContext>(options => options.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 27))));
public static void AnalyzeDbContext(this IServiceCollection services, string connectionString) =>
services.AddDbContext<AnalyzeDbContext>(options => options.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 27))));
.
.
.
但是当我尝试迁移它们时,它会显示如下豁免错误:
PM> Add-Migration init_1 -context AppDbContext
Build started...
Build succeeded.
Autofac.Core.DependencyResolutionException: An exception was thrown while activating WetaPayment.Infrastructure.Data.DataContext.AppDbContext.
---> Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[WetaPayment.Infrastructure.Data.DataContext.AppDbContext], MediatR.IMediator)' on type 'AppDbContext'.
---> System.TypeLoadException: Method 'AppendIdentityWhereCondition' in type 'MySql.EntityFrameworkCore.MySQLUpdateSqlGenerator' from assembly 'MySql.EntityFrameworkCore, Version=5.0.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' does not have an implementation.
最后,我修复了MySql.EntityFrameworkCore
与 net6.0 不兼容的更改包的部分问题,因此我使用Pomelo.EntityFrameworkCore.MySql
了它并且成功运行。
但是我还有另一个问题是我的所有聚合在所有上下文中都秃顶了,我需要每个 DbContext 只构建他的聚合,那么如何解决它?