gpt4 book ai didi

c# - 注册一个开放的通用服务错误地要求我输入类型参数

转载 作者:行者123 更新时间:2023-12-04 10:38:45 24 4
gpt4 key购买 nike

我有一个通用存储库,它采用一个通用 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 中注册此服务时,编译器根据 this link 要求我提供类型参数我不应该得到这个错误?

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

我得到的错误是这样的;

CS0305 Using the generic type 'IDataRepo' requires 2 type arguments

有人可以用这个给我指出正确的方向吗?

最佳答案

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

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

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

关于c# - 注册一个开放的通用服务错误地要求我输入类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60030434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com