简体   繁体   English

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

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

I have a Generic Repository which takes a Generic DbContext and a Generic Model,我有一个通用存储库,它采用通用 DbContext 和通用模型,

The Code I have is just a basic Generic Repository like so;我拥有的代码只是一个基本的通用存储库,就像这样;

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);
    }
}

when I try to register this Service in Startup.cs, the compiler asks me for type arguments when according to this link I shouldn't be getting this error?当我尝试在 Startup.cs 中注册此服务时,编译器要求我输入类型参数,而根据此链接,我不应该收到此错误?

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

The Error I get is this;我得到的错误是这样的;

CS0305 Using the generic type 'IDataRepo' requires 2 type arguments CS0305 使用泛型类型“IDataRepo”需要 2 个类型参数

Could someone point me in the right direction with this please?有人能指出我正确的方向吗?

Because you have multiple generic arguments, you will need to include a comma in the generic arguments因为您有多个泛型参数,所以您需要在泛型参数中包含一个逗号

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

to properly represent how many generic arguments are needed for the types involved正确表示所涉及的类型需要多少个泛型参数

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

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