1

我使用干净的拱形步骤来创建项目,但问题是我需要将三个以上的聚合放在参考数据库中。

我尝试对每个聚合使用 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 只构建他的聚合,那么如何解决它?

4

1 回答 1

0

每个人都DbContext可以配置自己的实体

假设我们有 10 个实体代表 3 DbContext,我们可以将它们分成 2 个实体ADbContext,5个实体BDbContext和 3个实体CDbContext。明智地管理它们之间的配置FK,否则将变得混乱。这里是如何做到这一点,使用流利的 API

public class MyAppDbContext : DbContext
{
    public DbSet<OurEntity> OurEntity { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<OurEntity>(builder => 
       {
            // Our implementation goes here...
       });
    }
}

像这样,我们只配置和注册与 each 相对应的实体DbContext

我们需要将每个DbContext单独注册到任何类型的 DI

为每个人组织迁移操作DbContext

我通常将它们组织在这样的文件夹中:

Data/Migrations/ContextA
Data/Migrations/ContextB
Data/Migrations/ContextC

每个上下文都有自己的迁移和快照。

DbContext通过在使用EF迁移工具时单独指定来执行此操作

add-migration add [MigrationName] -context [DbContextName] -o [Path to desire place to store migration]

然后分别应用它们

update-database -context [DbContextName]
于 2021-11-27T13:43:15.360 回答