【发布时间】:2022-08-18 16:14:30
【问题描述】:
我正在尝试注册多个(2)DbContexts 使用通用接口(@987654324@)实现。然后我想在我的控制器中使用一些逻辑来决定我将使用哪个上下文。
我在HomeController 中注入IEnumerable<IDbContext>,但它只用我注册的第一个实例来解决。
程序.cs
builder.Services.AddDbContext<IDbContext, ApplicationDbContext>(context => { context.UseInMemoryDatabase(\"ConferencePlanner\");});
builder.Services.AddDbContext<IDbContext, Application2DbContext>(context => { context.UseInMemoryDatabase(\"ConferencePlanner2\");});
DBContext1
public class ApplicationDbContext: DbContext, IDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options){}
}
数据库上下文2
public class Application2DbContext: DbContext, IDbContext
{
public Application2DbContext(DbContextOptions<Application2DbContext> options): base(options){}
}
IDbContext
public interface IDbContext {}
控制器:
private readonly IEnumerable<IDbContext> contexts;
public HomeController(ILogger<HomeController> logger, IEnumerable<IDbContext> contexts)
{
_logger = logger;
this.contexts = contexts;
}
-
我认为您也应该在 sturtup.cs 中通过 IEnumerable 。我的意思是:
builder.Services.AddDbContext<IEnumerable<IDbContext>, List<DbContext>>(YOUR_IMPLEMENTATION)或在您的控制器构造函数中放置 2 个上下文我的意思是:HomeController(IDbContext contexts1 , IDbContext> contexts2) -
AddDbContext 没有像你的例子那样的重载。它有这样的public static IServiceCollection AddDbContext<TContextService, TContextImplementation>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TContextImplementation : DbContext, TContextService;因为它不工作 -
这听起来可能是多租户设计。考虑使用 Scoped 或 Singleton Factory 依赖项来根据控制器等的需要构造/提供合适的 DbContext。 (即相关租户)
标签: c# .net entity-framework .net-core entity-framework-core