且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

异步方法,其包括异步调用和同步行动的结果

更新时间:2021-09-10 09:15:30

您不能等待多个查询在相同的环境。你必须使用每个操作的自己的上下文:

You cannot await multiple queries on the same context. You have to use an own context for each operation:

public async Task<long> GetSomethingFromDbAndSelectSomethingOnServer()
{
        using(var context = new MyEfDbContext())
        {
            // include the following if you do not need lazy loading and want some more speed
            context.Configuration.AutoDetectChangesEnabled = false;
            context.Configuration.ProxyCreationEnabled = false;

            var stuff = await myEfDbContext.StuffTable.ToListAsync();                     
            long itemsCount = stuff.Where(someQueryThatCantGoToDb).Count();
            return itemsCount;
        }
}