C# 如何正确配置'ConfigureServices'方法的'services.AddDbContext'

C# 如何正确配置'ConfigureServices'方法的'services.AddDbContext',c#,.net-core,entity-framework-core,dbcontext,C#,.net Core,Entity Framework Core,Dbcontext,我正在尝试使用EF Core运行.NET Core Web应用程序。为了测试存储库,我添加了一个MyDbContext,它继承了EFDbContext和interfaceIMyDbContext public interface IMyDbContext { DbSet<MyModel> Models { get; set; } } public class MyDbContext : DbContext, IMyDbContext { public MyDbCon

我正在尝试使用EF Core运行.NET Core Web应用程序。为了测试存储库,我添加了一个
MyDbContext
,它继承了EF
DbContext
和interface
IMyDbContext

public interface IMyDbContext
{
    DbSet<MyModel> Models { get; set; }
}

public class MyDbContext : DbContext, IMyDbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
    {
    }

    public virtual DbSet<MyModel> Models { get; set; }
}
public void ConfigureServices(IServiceCollection services)
{                    
    services.AddDbContext<MyDbContext>(options => options.UseSqlServer(...));
    services.AddScoped<IGenericRepository<MyModel>, GenericRepository<MyModel>>();
    services.AddScoped<IMyDbContext, MyDbContext>();
}
公共接口IMyDbContext
{
DbSet模型{get;set;}
}
公共类MyDbContext:DbContext,IMyDbContext
{
公共MyDbContext(DbContextOptions):基本(选项)
{
}
公共虚拟数据库集模型{get;set;}
}
上下文接口被注入到我的通用存储库中:

public class GenericRepository<TEntity> : IGenericRepository<TEntity>
{
    private readonly IMyDbContext _context = null;

    public GenericRepository(IMyDbContext context)
    {
        this._context = context;
    }
}
公共类GenericRepository:IGenericRepository
{
私有只读IMyDbContext _context=null;
公共通用存储库(IMyDbContext上下文)
{
这._context=context;
}
}
当我在startup.cs上使用此代码(无接口)时:

services.AddDbContext<MyDbContext>(options =>
     options.UseSqlServer(...));
services.AddDbContext(选项=>
选项。使用SQLServer(…);
我得到一个运行时错误:

InvalidOperationException:无法解析类型的服务 试图激活“GenericRepository`1[MyModel]”时出现“IMyDbContext”

当使用这行代码时:

services.AddDbContext<IMyDbContext>(options =>
     options.UseSqlServer(...));
services.AddDbContext(选项=>
选项。使用SQLServer(…);
我得到的编译时间错误代码是:

无法将lambda表达式转换为类型“ServiceLifetime”,因为它 不是委托类型

我的问题是如何正确配置
ConfigureServices
方法的
services.AddDbContext
(在
Configure
方法中是否需要任何更改?) 如果需要,我愿意修改IMyDbContext

使用having 2个泛型类型参数中的一个,它允许您指定要注册的服务接口/类以及实现它的
DbContext
派生类

例如:

services.AddDbContext<IMyDbContext, MyDbContext>(options =>
     options.UseSqlServer(...));
services.AddDbContext(选项=>
选项。使用SQLServer(…);
刚刚找到答案:

我没有在
IMyDbContext
MyDbContext
之间添加作用域

public interface IMyDbContext
{
    DbSet<MyModel> Models { get; set; }
}

public class MyDbContext : DbContext, IMyDbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
    {
    }

    public virtual DbSet<MyModel> Models { get; set; }
}
public void ConfigureServices(IServiceCollection services)
{                    
    services.AddDbContext<MyDbContext>(options => options.UseSqlServer(...));
    services.AddScoped<IGenericRepository<MyModel>, GenericRepository<MyModel>>();
    services.AddScoped<IMyDbContext, MyDbContext>();
}
public void配置服务(IServiceCollection服务)
{                    
services.AddDbContext(options=>options.UseSqlServer(…);
services.addScope();
services.addScope();
}

添加大括号以消除编译错误不要使用
IMyDbContext
,我也不会费心连接DbContext,而是使用AddDbContext@VidmantasBlazevicius我需要它来进行单元测试使用内存数据库进行单元测试Hey@Ivan,这看起来是一个干净而漂亮的答案,但它创建了一个异常:
无法为类型为“Startup”的方法“Configure”的参数“modelsDbContext”解析类型为“MyDbContext”的服务。内部异常1:InvalidOperationException:没有注册类型为“MyDbContext”的服务。
从EF核心的角度来看,以上就是我所能知道的。新的异常表示您在文章中未显示的代码中直接依赖于
MyDbContext