实际问题在于:
[Produces("application/json")]
[Route("api/[controller]")]
public class OrgController : Controller
{
private readonly IGenericRepository<OrgRepository> _repository;
public OrgController(IGenericRepository<OrgRepository> repo)
{
_repository = repo;
}
[HttpGet]
public async Task<IEnumerable<Org>> Get()
{
return await _repository.GetAllRecords();
}
}
}
GetAllRecords()行告诉我:
Error CS0029: Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<P5NCore.Repos itories.OrgRepository>>' to 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<P5NCore.Models.Org>>'(CS0029) (P5NCore)
GetallRecords()是实际返回正确类型的存储库代码:
public class OrgRepository : IGenericRepository<Org>
{
private readonly OrgContext _context = null;
public OrgRepository(IOptions<Settings> settings)
{
_context = new OrgContext(settings);
}
public async Task<IEnumerable<Org>> GetAllRecords(){
try{
return await _context.Orgs.Find(_=> true).ToListAsync();
}
catch (Exception ex){
throw ex;
}
}
...
}
Repo正在实施通用接口:
public interface IGenericRepository<T> where T:class
{
Task<IEnumerable<T>> GetAllRecords();
Task<T> GetRecord(string id);
Task AddRecord(T item);
}
我注册了这样的事情:
services.AddTransient<IGenericRepository<Org>, OrgRepository>();
我忽略了什么吗?