处理请求时发生未处理的异常。无效的操作异常:

时间:2018-07-16 14:20:36

标签: asp.net-core

无法从单例'ProjName范围内定义服务'ProjName.Contexts.ProductContext'。 Persistence.IProductRepository'

这是我的代码:

Public void ConfigurationService  (IServiceCollection service )
{
    service. AddDbContext <ProductContext>(options =>UserSqlServer (Configuration. GetConnectionString ("Prdcn")));
    Services. AddSingleton <IPRODUCTREPOSITORY, ProductRepository >(); 
}

1 个答案:

答案 0 :(得分:0)

没有太多信息可以继续,所以我只向您展示如何为DI注册上下文和存储库。

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<MyAppDbContext>(options =>
    {
        options.UseSqlServer(Configuration.GetConnectionString("LocalMachine"));
    });

    services.AddScoped<DbContext, MyAppDbContext>();
    services.AddScoped<IUnitOfWork, UnitOfWork>();
    //my repository is generic
    services.AddScoped<IRepository<IBaseEntity>, Repository>();

    ...
}