r3p*_*ica 4 c# linq asynchronous entity-framework async-await
我一直在将一些代码转换为异步方法.我有一个工作单元/存储库/服务设计模式,我的存储库看起来像这样:
public class Repository<T> : IDisposable, IRepository<T> where T : class
{
private readonly DbContext context;
private readonly DbSet<T> dbEntitySet;
public Repository(DbContext context)
{
if (context == null)
throw new ArgumentNullException("context");
this.context = context;
this.dbEntitySet = context.Set<T>();
}
public IQueryable<T> GetAll(params string[] includes)
{
IQueryable<T> query = this.dbEntitySet;
foreach (var include in includes)
query = query.Include(include);
return query;
}
public void Create(T model)
{
this.dbEntitySet.Add(model);
}
public void Update(T model)
{
this.context.Entry<T>(model).State = EntityState.Modified;
}
public void Remove(T model)
{
this.context.Entry<T>(model).State = EntityState.Deleted;
}
public void Dispose()
{
this.context.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
在这个类中,我想让我的GetAll方法异步.我发现一篇文章将此作为一种方法:
public async Task<List<T>> GetAllAsync()
{
return await this.dbEntitySet.ToListAsync();
}
Run Code Online (Sandbox Code Playgroud)
这一切都很好,花花公子,但我需要在向用户返回任何内容之前添加字符串[] include.所以我决定也许我应该单独留下Repository并专注于服务,所以我有这个方法:
public IList<User> GetAllAsync(params string[] includes)
{
return this.Repository.GetAll(includes).ToList();
}
Run Code Online (Sandbox Code Playgroud)
我试图改变这个:
public async Task<List<User>> GetAllAsync(params string[] includes)
{
return await this.Repository.GetAll(includes).ToListAsync();
}
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:
错误1'System.Linq.IQueryable'不包含'ToListAsync'的定义,并且没有可以找到接受类型为'System.Linq.IQueryable'的第一个参数的扩展方法'ToListAsync'(您是否缺少using指令或装配参考?)
有人能指出我正确的方向吗?
| 归档时间: |
|
| 查看次数: |
3588 次 |
| 最近记录: |