【发布时间】:2017-12-16 07:16:21
【问题描述】:
我有以下代码:
public Task<Service> GetSomething()
{
using (var myContext = new DbContext())
{
var returnObj = (from rp in myContext.Services1
join op in myContext.Services2 on rp .Id equals op.ServiceId into g
join ep in myContext.Services3 on rp .Id equals ep.ServiceId
from n in g.DefaultIfEmpty()
where rp.Name == code
select rp).FirstOrDefaultAsync();
return returnObj;
}
}
现在这正在工作,但我遇到了错误:
The operation cannot be completed because the DbContext has been disposed.
阅读后,看起来FirstOrDefaultAsync 是一个延迟执行,我需要先将其转换为list 以使其具体化。
我将如何转换此查询的结果,因为我尝试了.ToListAsync(),但它之后没有任何FirstOrDefault。
【问题讨论】:
-
哪一行抛出异常?您是否启用了延迟加载?如果您进行了@Yeldar Kurmangaliyev 建议的更改并且仍然得到处理异常,那么我只能认为延迟加载是原因。
标签: c# entity-framework asynchronous