C# 异步EntityFramework操作

C# 异步EntityFramework操作,c#,linq,entity-framework,asynchronous,async-await,C#,Linq,Entity Framework,Asynchronous,Async Await,我一直在将一些代码转换为异步方法。 我有一个工作单元/存储库/服务设计模式,我的存储库如下所示: public class Repository<T> : IDisposable, IRepository<T> where T : class { private readonly DbContext context; private readonly DbSet<T> dbEntitySet; public Repository(Db

我一直在将一些代码转换为异步方法。 我有一个工作单元/存储库/服务设计模式,我的存储库如下所示:

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();
    }
}
公共类存储库:IDisposable,IRepository其中T:class
{
私有只读DbContext上下文;
私有只读DbSet dbEntitySet;
公共存储库(DbContext上下文)
{
if(上下文==null)
抛出新的ArgumentNullException(“上下文”);
this.context=上下文;
this.dbEntitySet=context.Set();
}
公共IQueryable GetAll(参数字符串[]包括)
{
IQueryable query=this.dbEntitySet;
foreach(包含在包含中的var)
query=query.Include(包含);
返回查询;
}
公共void创建(T模型)
{
this.dbEntitySet.Add(model);
}
公共无效更新(T型)
{
this.context.Entry(model).State=EntityState.Modified;
}
公共空间移除(T型)
{
this.context.Entry(model).State=EntityState.Deleted;
}
公共空间处置()
{
this.context.Dispose();
}
}
在这个类中,我想使我的GetAll方法异步。我发现一篇文章将此作为一种方法:

public async Task<List<T>> GetAllAsync()
{
    return await this.dbEntitySet.ToListAsync();
}
公共异步任务GetAllAsync() { return wait this.dbEntitySet.ToListSync(); } 这一切都很好,但我需要在向用户返回任何内容之前添加字符串[]includes。因此,我决定也许我应该离开存储库,专注于服务,因此我有以下方法:

public IList<User> GetAllAsync(params string[] includes)
{
    return this.Repository.GetAll(includes).ToList();
}
public IList GetAllAsync(参数字符串[]包括)
{
返回此.Repository.GetAll(includes).ToList();
}
我尝试将其更改为:

public async Task<List<User>> GetAllAsync(params string[] includes)
{
    return await this.Repository.GetAll(includes).ToListAsync();
}
公共异步任务GetAllAsync(参数字符串[]包括)
{
return wait this.Repository.GetAll(includes).toListSync();
}
但我有一个错误:

错误1“System.Linq.IQueryable”不包含“ToListSync”的定义,并且找不到接受“System.Linq.IQueryable”类型的第一个参数的扩展方法“ToListSync”(是否缺少using指令或程序集引用?)


有人能给我指出正确的方向吗?

正如@mostruash所指出的,如果我将使用System.Data.Entity放入我的类引用中,它会编译并运行良好。

根据这一点,如果您使用的是
System.Data.Entity
,并且如果您真正使用的是EF6,那么就不可能得到那个错误。再次检查dll版本以确保。我建议从所有项目中卸载/重新安装EF6 NuGet包(如果是多项目的话)。我这样做了,但仍然遇到同样的问题:(我得到了纠正;它已编译:对于我的情况,我有类似的ANYASNC()但我无法在Visual Studio的帮助下解决此问题,请投票支持将我链接到解决方案,并感谢@mostruash