我使用了干凈的拱形步驟來創建專案,但問題是我有超過三個聚合,我需要將它們放入參考資料庫中。
我嘗試為每個聚合使用 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 只構建他的聚合,那么如何修復它?
uj5u.com熱心網友回復:
每個人都DbContext可以配置自己的物體
假設我們有 10 個物體 3 DbContext,我們可以將它們分成 2 個物體ADbContext,5個物體BDbContext和 3個物體CDbContext。FK明智地管理它們之間的配置,否則,那將變得混亂。這是使用 fluent 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...
});
}
}
像那樣,我們只配置和注冊與每個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]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/368456.html
標籤:C# 实体框架 asp.net核心 领域驱动设计 ardalis-cleanarchitecture
上一篇:升級到.net后連接到Db
