los*_*nos 3 .net c# entity-framework async-await asp.net-core
我正在尝试使我的通用基础存储库全部异步,并且我有代码:
public IEnumerable<T> GetAll()
{
return _dbContext.Set<T>();
}
public IQueryable<T> GetQueryable()
{
return _dbContext.Set<T>();
}
Run Code Online (Sandbox Code Playgroud)
既然没有诸如 之类的方法SetAsync,我怎样才能使这些方法异步呢?
对于返回数据的方法,您可以使它们异步:
public IAsyncEnumerable<T> GetAll()
{
return _dbContext.Set<T>().AsAsyncEnumerable();
}
Run Code Online (Sandbox Code Playgroud)
或者
public async Task<IList<T>> GetAll()
{
return await _dbContext.Set<T>().ToListAsync();
}
Run Code Online (Sandbox Code Playgroud)
但您不希望此方法是异步的。
public IQueryable<T> GetQueryable()
{
return _dbContext.Set<T>();
}
Run Code Online (Sandbox Code Playgroud)
因为它不返回数据或执行数据库访问。它只是为调用者提供了一个可以使用的存根查询。因此调用者将运行一个异步查询,例如:
var orders = await customerRepo.GetQueryable().Where( o => o.CustomerId = 123 ).ToListAsync();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6067 次 |
| 最近记录: |