使用多个参数注册DbContext [英] registering DbContext with multiple parameters
问题描述
我正在尝试将TenantProvider注入DbContext
公共类AppDbContext:IdentityDbContext< ApplicationUser,ApplicationRole,long>
{
public int? _tenantId;
公共ITenantProvider _tenantProvider;
public AppDbContext(
DbContextOptions< AppDbContext>选项,
ITenantProvider tenantProvider
)
:基本(选项)
{
_tenantProvider = tenantProvider;
}
但我不知道如何正确注册-如果我将构造函数中的断点- tenantProvider
为 null
。
Startup.cs
services.AddDbContext< AppDbContext>(选项=> AppDbContextOptionsBuilder.Get());
下一行需要注入 DbContext
添加到控制器或服务中(如果我将 ServiceLifetime.Scoped
添加为上述方法的第二个参数- AddDbContext
-该功能无效):
services.AddScoped(p => new AppDbContext(AppDbContextOptionsBuilder.Get(), p.GetService< ITenantProvider>()));
( Entity Framework
是我的解决方案)
使用 .AddScoped
方法时-通过使用 .GetService
方法解析 TenantProvider
进入构造函数。
有人知道如何在 .AddDbContext
方法中解析 TenantProvider
吗?
其他信息:
我正尝试替换 DbContext
的构造函数中的 ITenantProvider
和 IHttpContextAccessor
-后者注册为单例。但是 acessor
参数仍然为 null
。
services.AddSingleton< IHttpContextAccessor,HttpContextAccessor>();
我不不太了解您的 AddScoped
调用应该做什么。 AddDbContext
已经在服务集合中正确注册了数据库上下文。因此,当您通过依赖项注入解决上下文时,其他依赖项将被自动解决。
因此,这样做应该足够了:
services.AddDbContext< AppDbContext>(options =>…);
服务。AddSingleton< ITenantProvider,TenantProvider>();
然后,您可以依赖您的 AppDbContext
使用构造函数注入,例如
不过有两个注释:
-
在配置选项时,应修改所传递的选项对象。因此,您不应该只返回
AppDbContextOptionsBuilder.Get()
,而是使用传递的选项对象并对其进行编辑。 -
您应该真正考虑依赖于租户提供者的数据库上下文是否正确。根据 SRP ,您的数据库只能做一件事,并且可以提供数据库访问权限。 / p>
根据租户提供者如何影响数据库访问,将这种依赖关系上移到同时使用数据库上下文和租户提供者的某些服务中可能更有意义以正确的方式查询数据。
I'm trying to inject TenantProvider into DbContext
public class AppDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, long>
{
public int? _tenantId;
public ITenantProvider _tenantProvider;
public AppDbContext(
DbContextOptions<AppDbContext> options,
ITenantProvider tenantProvider
)
: base(options)
{
_tenantProvider = tenantProvider;
}
but I don't understand how to register it correctly - if I put the breakpoint in the constructor - tenantProvider
is null
.
The bit from Startup.cs
services.AddDbContext<AppDbContext>(options => AppDbContextOptionsBuilder.Get());
the next line is required to inject the DbContext
into a controller or a service (if I add ServiceLifetime.Scoped
as a second parameter to the method above - AddDbContext
- the feature doesn't work):
services.AddScoped(p => new AppDbContext(AppDbContextOptionsBuilder.Get(), p.GetService<ITenantProvider>()));
(Entity Framework
is a separate project in my solution)
When using .AddScoped
method - we can pass TenantProvider
into constructor by resolving it using .GetService
method.
Does anyone have an idea of how to resolve TenantProvider
in .AddDbContext
method?
Additional info:
I was trying to replace ITenantProvider
in the constructor of DbContext
with IHttpContextAccessor
- the latter is registered as singleton. But the acessor
parameter is still null
.
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
I don’t really understand what your AddScoped
call is supposed to do. AddDbContext
will already register the database context properly with the service collection. So when you resolve the context through dependency injection, additional dependencies will be automatically resolved.
So it should be enough to do this:
services.AddDbContext<AppDbContext>(options => …);
services.AddSingleton<ITenantProvider, TenantProvider>();
And then, you can depend on your AppDbContext
using constructor injection, e.g. in your controllers.
Two notes though:
When configuring options, you should modify the passed options object. So you should not just return
AppDbContextOptionsBuilder.Get()
but instead use the passed options object and edit that.You should really think about whether your database context having a dependency on your tenant provider is the right thing to do. As per SRP, your database should only do a single thing and that is provide database access.
Depending on how your tenant provider affects your database access, it might make more sense to move this dependency up one level into some service that uses both the database context and the tenant provider to query data in the right way.
这篇关于使用多个参数注册DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!