基于Linq联接,我得到错误在先前操作完成之前,在此上下文中启动了第二个操作 [英] Based on a Linq join I get the error A second operation started on this context before a previous operation completed
问题描述
以下代码使用dbcontext.
The following code uses dbcontext.
var CurrentUser = await userManager.GetUserAsync(User);
var DefaultLanguageID = CurrentUser.DefaultLangauge;
var TestForNull = (from c in _classificationLevel.GetAllClassificationLevels()
join l in _classificationLevelLanguage.GetAllClassificationLevelLanguages()
on c.Id equals l.ClassificationLevelId
where c.ClassificationId == NewLevel.SuObject.ObjectId
&& l.LanguageId == DefaultLanguageID
select c.Sequence).Count();
运行它时,出现错误
A second operation started on this context before a previous operation completed.
我不确定为什么.我可以想象第一个操作是: userManager.GetUserAsync(User);
I am not sure why. I can imagine that the first operation is: userManager.GetUserAsync(User);
,并且在下一次启动时仍在运行.尽管它的关键字在前面.
and is still running while the next starts. Though it has the keyword await in front.
我认为的另一个选择是在查询中检索到两个表,并将其用于相同的dbcontext. 1. _classificationLevel.GetAllClassificationLevels() 2. _classificationLevelLanguage.GetAllClassificationLevelLanguages()
The other option I thought is that within the query two tables are retrieved and that it uses for both the same dbcontext. 1. _classificationLevel.GetAllClassificationLevels() 2. _classificationLevelLanguage.GetAllClassificationLevelLanguages()
这是错误的原因吗?
这些方法背后的方法是:
The methods behind these are:
public IEnumerable<SuClassificationLevelModel> GetAllClassificationLevels()
{
return context.dbClassificationLevel.AsNoTracking();
}
和
public IEnumerable<SuClassificationLevelLanguageModel> GetAllClassificationLevelLanguages()
{
return context.dbClassificationLevelLanguage;
}
我没有异步/等待这些.如果应该的话,我应该怎么做?
I don't have an async / await on these. Should I and if so, how should I do that?
在模型级别,两个模型是相关的,如下面的代码所示:
On a model level, the two models are related as shown in the code below:
public interface IClassificationAndStatusRepository
{
IEnumerable<SuObjectVM> GetAllClassifications();
IEnumerable<SuClassificationStatusModel> GetAllStatus();
}
推荐答案
调用的代码是同一模型的2倍:
The code called 2 times the same model:
var testForNull = await (from c in GetAllClassificationLevels()
join l in GetAllClassificationLevelLanguages()
on c.Id equals l.ClassificationLevelId
where c.ClassificationId == NewLevel.SuObject.ObjectId
&& l.LanguageId == defaultLanguageID
select c.EmployeeID).CountAsync();
GetAllClassificationLevelLanguages()和GetAllClassificationLevels()已包含外键表.因此,两者都调用相同的2个表.
GetAllClassificationLevelLanguages() and GetAllClassificationLevels() already include the foreign key tables. Thus both call the same 2 tables.
这篇关于基于Linq联接,我得到错误在先前操作完成之前,在此上下文中启动了第二个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!