c# - 实体框架 - 实例化存储库包装器时不明确的 DBContext 构造函数 - 使用带参数的 DbContext
问题描述
我带着这个在圈子里跑来跑去。
当解决方案尝试在存储库包装器中实例化 dbContext 时,我收到以下错误。
-
InnerException {"Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.IDATARepositoryWrapper Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.DATARepositoryWrapper': Unable to activate type 'JobsLedger.DATA.DATAContext'. The following constructors are ambiguous:\r\nVoid .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[JobsLedger.DATA.DATAContext])\r\nVoid .ctor(JobsLedger.INTERFACES.IDbConnectionString)"} System.Exception {System.InvalidOperationException}
正如它所说,三个 dbcontext 构造函数中的两个存在歧义。
以下是构造函数:
// Constructors..
// This is for the migration. OnConfiguring is setup to ignore both other constructors and go right through to
// base.(onConfiguring) without using the other constructor's parameters...
public DATAContext(DbContextOptions<DATAContext> options) : base(options)
{
}
这是我的 OnConfiguring 方法。
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (this.HttpContextAccessor != null)
{
var httpContext = this.HttpContextAccessor.HttpContext;
var httpContextConnectionString = httpContext.Items["connectionString"].ToString();
optionsBuilder.UseSqlServer(httpContextConnectionString ?? throw new InvalidOperationException());
}
else
{
if (_connectionString != null)
{
optionsBuilder.UseSqlServer(_connectionString.ConnectionString ?? throw new InvalidOperationException());
}
}
base.OnConfiguring(optionsBuilder);
}
// This is for the creation of a new database as per NewDataBaseCreationService.
public DATAContext(IDbConnectionString connectionString)
{
_connectionString = connectionString;
}
// This is when the account holder has logged in and the HttpContext has the connectionstring attached to as an item.
public DATAContext(IHttpContextAccessor httpContextAccessor)
{
this.HttpContextAccessor = httpContextAccessor;
}
..这里是上面那个错误的地方......
public class DATARepositoryWrapper: IDATARepositoryWrapper
{
private readonly DATAContext _DATAContext;
public DATARepositoryWrapper(DATAContext dataContext)
{
_DATAContext = dataContext;
}
我想用 HttpContextAccessor 注入 DATAContext ..
如何使用 DbContext 和参数实例化包装器?
解决方案
您可以使用下面的代码注入 HttpContextAccessor。
services.AddHttpContextAccessor();
然后您应该使用以服务提供者为输入的 AddDbContext 方法覆盖,这将有助于获取 HttpContextAccessor 对象并访问 HttpContext 以从 Items 获取连接字符串。如果连接字符串在项目中不可用,则使用默认连接字符串。
services.AddDbContext<DATAContext>((serviceProvider, dbContextBuilder) =>
{
var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
object connectionString = string.Empty;
if (httpContextAccessor.HttpContext.Items.TryGetValue("connectionString", out connectionString))
{
dbContextBuilder.UseNpgsql((string)connectionString);
return;
}
dbContextBuilder.UseNpgsql(defaultConnectionString);
}, ServiceLifetime.Scoped);
因此,现在您只需要一个以 DbContextOptions 作为参数的构造函数。
public DATAContext(DbContextOptions options)
: base(options)
{}
推荐阅读
- c++ - 对于关键部分 - 何时使用 std::mutex 与 std::atomic_flag?
- flutter - 将数据发送到新屏幕,但不知道如何设置
- outlook - 尝试使用 Microsoft Graph 获取/设置 messagerules API 时出现 ErrorAccessDenied
- google-apps-script - 具有动态模式的数据洞察连接器
- c# - 我可以简化我的 ConcurrentDictionary “添加或更新”方法吗?
- python - 有没有办法不使用mrjob在reducer def中包含第三个参数?
- python-3.x - 使用 SEQ 在 python 中记录 Azure 函数
- javascript - 是否有必要在每个连接上调用 response.end() ?
- iis - 如何使用 IdentityServer4 下的进程内 IIS 托管启用 Windows 身份验证?
- c++ - 访问 NS2 数据包头以获取无线网络中的接收数据