【问题标题】:ASP .NET CORE 1.1.1 Dependency Injection ErrorASP .NET CORE 1.1.1 依赖注入错误
【发布时间】:2017-11-23 22:32:39
【问题描述】:

我是 Asp .Net Core 的新手,并使用它创建了 App。我在我的项目中使用通用存储库。但我有一个错误:

在尝试激活“ECommerce.Repository.ProductRepository”时无法解析“Microsoft.EntityFrameworkCore.DbContext”类型的服务。

BaseRepository

protected DbContext _dbContext;
    protected readonly DbSet<T> _dbSet;

    public BaseRepository(DbContext dbContext)
    {
        _dbContext = dbContext;
        _dbSet = dbContext.Set<T>();
    }

存储库

public partial class ProductRepository : BaseRepository<Product>, IProductRepository
{
    public ProductRepository(DbContext dbContext) : base(dbContext) { }
}

服务

public partial class ProductService : BaseService<Product>, IProductService
{
    private readonly IProductRepository _repository;
    private readonly IProductValidation _validation;
    private readonly IUnitOfWork _unitOfWork;
    public ProductService(IProductValidation validation, IProductRepository respository, IUnitOfWork unitOfWork)
        : base(validation, respository, unitOfWork)
    {
        _repository = respository;
        _validation = validation;
        _unitOfWork = unitOfWork;
    }
}

验证

public partial class ProductValidation : BaseValidation<Product>, IProductValidation
{
    private readonly IProductRepository _productRepository;

    public ProductValidation(IProductRepository productRepository) : base(productRepository)
    {
    }
}

启动

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ECommerceDbContext>(options =>
           options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        // Add framework services.
        services.AddMvc();


        services.AddTransient<IUnitOfWork, UnitOfWork>();
        services.AddTransient<IProductRepository, ProductRepository>();
        services.AddTransient<IProductService, ProductService>();
        services.AddTransient<IProductValidation, ProductValidation>();
    }

控制器

private readonly IProductService _productService;

    public ValuesController(IProductService productService)
    {
        _productService = productService;
    }
    // GET api/values
    [HttpGet]
    public IEnumerable<Product> Get()
    {
        return _productService.GetAll();
    }

请告诉我我的代码有什么问题。非常感谢

P/s:这段代码在我之前使用 Autofac 使用 Asp .Net 4.6 的项目中完美运行

【问题讨论】:

    标签: c# dependency-injection asp.net-core entity-framework-core asp.net-core-webapi


    【解决方案1】:

    根据您对其他答案的 cmets:

    如果您确定您的应用程序中只有 ONE DbContext,您可以这样做

    services.AddScoped<DbContext, ECommerceDbContext>();
    

    services.AddScoped<DbContext>(provider => provider.GetRequiredService<ECommerceDbContext>());
    

    如果您不希望 DbContextECommerceDbContext 解析为两个不同的实例

    【讨论】:

    • 你能解释更详细的区别2方法吗?
    • 就像我在帖子中所说的那样,第一个将在两种不同的情况下解决。如果注入SomeConstructor(DbContext context, ECommerceDbContext eContext),那么context == eContext 将评估为假,因为它们是不同的。对于上述示例,第二种方法的计算结果应为等于
    【解决方案2】:

    您需要注入实际的 DbContext 类,在本例中为 ECommerceDbContext

    所以把构造函数改成:

    public BaseRepository(ECommerceDbContext dbContext)
    {
        _dbContext = dbContext;
        _dbSet = dbContext.Set<T>();
    }
    

    【讨论】:

    • 我做不到。项目名称 Core 中的 BaseRepository 和我的应用程序通过 DLL 使用它。我想在另一个项目中使用它,我不想改变它。
    • 由于它们不提供允许您设置服务类型的重载,因此您必须复制 EF Core 在 AddDbContext() 中所做的操作,并将其更改为服务类型为 DbContext而不是上下文类型:github.com/aspnet/EntityFramework/blob/…
    • 我将 DbContext 注册到 ECommerceDbContext,它解决了我的问题..:)
    • 哦。等待。哈哈。我只是个笨蛋。 :P
    猜你喜欢
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 2020-12-20
    • 2018-03-03
    相关资源
    最近更新 更多