我嘗試使用 Multiple DbContext 將我專案的每個域遷移到不同的資料庫,如下所示:
public class AppDbContext: DbContext
.
.
public class UserAccessDbContext: DbContext
.
.
public class AdministrationDbContext: DbContext
.
.
Etc
但看起來我做錯了什么,所以我得到了每個 DbContext 中的所有表,我沒有任何鏈接域的 FK。
我的每個 DbContext 的連接字串是這樣的:
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 UserAccessDbContext(this IServiceCollection services, string connectionString) =>
services.AddDbContext<UserAccessDbContext>(options => options.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 27))));
public static void AdministrationDbContext(this IServiceCollection services, string connectionString) =>
services.AddDbContext<AdministrationDbContext>(options => options.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 27))));
我的存盤庫是這樣的:
public class AppRepository<T> : RepositoryBase<T>, IReadRepository<T>, IRepository<T> where T : class, IAggregateRoot
{
public AppRepository(AppDbContext appDbContext) : base(appDbContext)
{
}
}
.
.
.
public class AdministrationRepository<T> : RepositoryBase<T>, IReadRepository<T>, IRepository<T> where T : class, IAggregateRoot
{
public AdministrationRepository(AdministrationDbContext administrationdbContext) : base(administrationdbContext)
{
}
}
.
.
.
public class UserAccessRepository<T> : RepositoryBase<T>, IReadRepository<T>, IRepository<T> where T : class, IAggregateRoot
{
public UserAccessRepository(UserAccessDbContext userAccessdbContext) : base(userAccessdbContext)
{
}
}
我還像這樣注冊了 repo:
public class DefaultInfrastructureModule : Module
{
private readonly bool _isDevelopment = false;
private readonly List<Assembly> _assemblies = new List<Assembly>();
public DefaultInfrastructureModule(bool isDevelopment, Assembly? callingAssembly = null)
{
_isDevelopment = isDevelopment;
var coreAssembly = Assembly.GetAssembly(typeof(User)); // TODO: Replace "Project" with any type from your Core project
var infrastructureAssembly = Assembly.GetAssembly(typeof(StartupSetup));
if (coreAssembly != null)
{
_assemblies.Add(coreAssembly);
}
if (infrastructureAssembly != null)
{
_assemblies.Add(infrastructureAssembly);
}
if (callingAssembly != null)
{
_assemblies.Add(callingAssembly);
}
}
protected override void Load(ContainerBuilder builder)
{
if (_isDevelopment)
{
RegisterDevelopmentOnlyDependencies(builder);
}
else
{
RegisterProductionOnlyDependencies(builder);
}
RegisterCommonDependencies(builder);
}
private void RegisterCommonDependencies(ContainerBuilder builder)
{
//Start Repository Reg
builder.RegisterGeneric(typeof(AppRepository<>))
.As(typeof(IRepository<>))
.As(typeof(IReadRepository<>))
.InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(AdministrationRepository<>))
.As(typeof(IRepository<>))
.As(typeof(IReadRepository<>))
.InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(UserAccessRepository<>))
.As(typeof(IRepository<>))
.As(typeof(IReadRepository<>))
.InstancePerLifetimeScope();
//End Repository Reg
builder
.RegisterType<Mediator>()
.As<IMediator>()
.InstancePerLifetimeScope();
builder.Register<ServiceFactory>(context =>
{
var c = context.Resolve<IComponentContext>();
return t => c.Resolve(t);
});
var mediatrOpenTypes = new[]
{
typeof(IRequestHandler<,>),
typeof(IRequestExceptionHandler<,,>),
typeof(IRequestExceptionAction<,>),
typeof(INotificationHandler<>),
};
foreach (var mediatrOpenType in mediatrOpenTypes)
{
builder
.RegisterAssemblyTypes(_assemblies.ToArray())
.AsClosedTypesOf(mediatrOpenType)
.AsImplementedInterfaces();
}
builder.RegisterType<EmailSender>().As<IEmailSender>()
.InstancePerLifetimeScope();
}
private void RegisterDevelopmentOnlyDependencies(ContainerBuilder builder)
{
// TODO: Add development only services
}
private void RegisterProductionOnlyDependencies(ContainerBuilder builder)
{
// TODO: Add production only services
}
}
我在我的代碼中錯過了什么來修復它!
任何幫助我該怎么做。
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 組織遷移操作
我通常將它們組織在這樣的檔案夾中:
- 資料/遷移/背景關系A
- 資料/遷移/背景關系B
- 資料/遷移/背景關系C
每個背景關系都有自己的遷移和快照。
通過在使用 EF 遷移工具時指定單獨的 DbContext 來做到這一點
dotnet ef migrations add [MigrationName] --context [DbContextName] --output-dir [Path to desire place to store migration]
然后分別應用它們
dotnet ef database update [MigrationName] --context [DbContextName]
就是這樣。當然我們可以做到,但我還是建議如果沒有具體原因,我們應該避免這種方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/367637.html
標籤:C# 实体框架 asp.net核心 领域驱动设计 .net-6.0
下一篇:為什么我們在DbContext中使用DependecyInjection而不是OnConfiguringMethod?
