繁体   English   中英

注册一个开放的通用服务错误地要求我提供类型参数

[英]Registering an open Generic Service erroneously asks me for type arguments

我有一个通用存储库,它采用通用 DbContext 和通用模型,

我拥有的代码只是一个基本的通用存储库,就像这样;

public class DataRepo<T,M> : IDataRepo<T,M> where T:DbContext where M :class
{
    private readonly T _Context;
    private readonly M _Model;


    public DataRepo(T Context, M model)
    {
        _Context = Context;
        _Model = model;
    }
    public void Delete()
    {
       _Context.Remove(_Model);
    }

    public async Task<IEnumerable<T>> GetAll()
    {
        var results = await _Context.Set<T>().AsQueryable().AsNoTracking().ToListAsync();

        return results;
    }

    public T GetSingleByID(int ID)
    {
        return _Context.Find<T>(ID);            
    }

    public void InsertBulkItems(ICollection<M> dataModels)
    {
        _Context.AddRange(dataModels);
    }

    public void InsertSingleItem()
    {
        _Context.Add(_Model);
    }

    public void UpdateItem()
    {
        _Context.Update(_Model);
    }
}

当我尝试在 Startup.cs 中注册此服务时,编译器要求我输入类型参数,而根据此链接,我不应该收到此错误?

services.AddScoped(typeof(IDataRepo<>), typeof(DataRepo<>));

我得到的错误是这样的;

CS0305 使用泛型类型“IDataRepo”需要 2 个类型参数

有人能指出我正确的方向吗?

因为您有多个泛型参数,所以您需要在泛型参数中包含一个逗号

services.AddScoped(typeof(IDataRepo<,>), typeof(DataRepo<,>));

正确表示所涉及的类型需要多少个泛型参数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM