C# 通过EFCore 3中的接口解析DbContext

C# 通过EFCore 3中的接口解析DbContext,c#,dependency-injection,.net-core,dbcontext,ef-core-3.0,C#,Dependency Injection,.net Core,Dbcontext,Ef Core 3.0,通过接口注册/解析DbContext的正确方法是什么 背景: 我有多个web服务,每个web服务具有不同的dbcontext 因为我需要在一个共享项目中编写一些公共功能,所以我编写了一个由每个dbcontext实现的接口,这样我就可以将共享功能集“dbcontext实现无关”注入dbcontext接口 在每个项目中,每个dbcontext都已经被它的实现使用和注入了,但是我只想在commonfeatures项目中使用它的接口 因此,在每个Web服务中,我都会有如下内容: services.Ad

通过接口注册/解析DbContext的正确方法是什么


背景: 我有多个web服务,每个web服务具有不同的dbcontext

因为我需要在一个共享项目中编写一些公共功能,所以我编写了一个由每个dbcontext实现的接口,这样我就可以将共享功能集“dbcontext实现无关”注入dbcontext接口

在每个项目中,每个dbcontext都已经被它的实现使用和注入了,但是我只想在commonfeatures项目中使用它的接口

因此,在每个Web服务中,我都会有如下内容:

services.AddDbContext<IMyDbcontext, MyDbcontext>(options =>
{
    options.UseSqlServer(configuration.GetConnectionString("MyDbContext"));
});
services.AddTransient<ISharedService, SharedService>();
IMyDbcontext
就像

public interface IMyDbcontext
{
    DbSet<SharedSettings> Settings { get; set; }

    int SaveChanges(bool acceptAllChangesOnSuccess);
    int SaveChanges();
    Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default);
    Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}

我还尝试像这样注册dbcontext

services.AddDbContext<IMyDbContext, MyDbContext>();
services.AddDbContext();
但是,当我试图解析
MyDbContext
时,我得到了
null
,我不明白为什么,正如下面所指出的:

IMyService
需要一个限定范围的
IMyDbContext
,但在您的 startups Configure()方法您没有作用域

您可以将您的
IDbContext
设置为瞬态(如果您可以接受的话) 你)

或者,您可以尝试使用
CreateScope()
方法,然后从该新作用域获取IMyService 提供者

所以,我用IServiceScopeFactory创建了一个范围,它成功了

var scopeFactory = app.ApplicationServices.GetService<IServiceScopeFactory>();   
using var scope = scopeFactory.CreateScope();
var myService = scope.ServiceProvider.GetService<IMyService>();
var scopeFactory=app.ApplicationServices.GetService();
使用var scope=scopeFactory.CreateScope();
var myService=scope.ServiceProvider.GetService();
正如以下人士所指出的:

IMyService
需要一个限定范围的
IMyDbContext
,但在您的 startups Configure()方法您没有作用域

您可以将您的
IDbContext
设置为瞬态(如果您可以接受的话) 你)

或者,您可以尝试使用
CreateScope()
方法,然后从该新作用域获取IMyService 提供者

所以,我用IServiceScopeFactory创建了一个范围,它成功了

var scopeFactory = app.ApplicationServices.GetService<IServiceScopeFactory>();   
using var scope = scopeFactory.CreateScope();
var myService = scope.ServiceProvider.GetService<IMyService>();
var scopeFactory=app.ApplicationServices.GetService();
使用var scope=scopeFactory.CreateScope();
var myService=scope.ServiceProvider.GetService();

您能否提供当前启动和相关类的更详细示例。不清楚您是否需要接口或实现。您想做什么?如果要同时解析
MyDbContext
IMyDbContext
,则必须注册这两种类型
AddDbContext
仅使用
MyDbContext
提供的实现注册
IMyDbContext
。不过,只需注册
MyDbContext
就足够了-任何需要
MyDbContext
的代码都会得到一个,而且永远不需要知道数据来自何处,例如数据库或字典。我添加了更多的context。您可以展示当前启动和相关类的更详细示例吗。不清楚您是否需要接口或实现。您想做什么?如果要同时解析
MyDbContext
IMyDbContext
,则必须注册这两种类型
AddDbContext
仅使用
MyDbContext
提供的实现注册
IMyDbContext
。不过,只需注册
MyDbContext
就足够了-任何需要
MyDbContext
的代码都会得到一个,而且永远不需要知道数据来自何处,例如数据库或字典。我添加了更多的上下文
public MyService(IMydbcontext context)
services.AddDbContext<IMyDbContext, MyDbContext>();
var scopeFactory = app.ApplicationServices.GetService<IServiceScopeFactory>();   
using var scope = scopeFactory.CreateScope();
var myService = scope.ServiceProvider.GetService<IMyService>();