-1

以下代码使用 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);

并且在下一次启动时仍在运行。虽然它前面有关键字 await 。

我认为的另一个选项是在查询中检索两个表,并将其用于相同的 dbcontext。1. _classificationLevel.GetAllClassificationLevels() 2. _classificationLevelLanguage.GetAllClassificationLevelLanguages()

这是错误的原因吗?

这些背后的方法是:

public IEnumerable<SuClassificationLevelModel> GetAllClassificationLevels()
{
    return context.dbClassificationLevel.AsNoTracking();
}

public IEnumerable<SuClassificationLevelLanguageModel> GetAllClassificationLevelLanguages()
{
    return context.dbClassificationLevelLanguage;
}

我没有 async / await 这些。我应该,如果是,我应该怎么做?

在模型级别上,这两个模型是相关的,如下面的代码所示:

public interface IClassificationAndStatusRepository
{
    IEnumerable<SuObjectVM> GetAllClassifications();
    IEnumerable<SuClassificationStatusModel> GetAllStatus();
}
4

2 回答 2

0

您必须等待所有后续步骤才能正确使用 aync/await 结构。

您可以在您的情况下使用CountAsync()但异步计数只是 IQueryable 的扩展,因此您可能必须更改方法返回类型 IQueryable ,它更快更安全

    public IQueryable<SuClassificationLevelModel> GetAllClassificationLevels()
    {
        return context.dbClassificationLevel;
    }

    public IQueryable<SuClassificationLevelLanguageModel> GetAllClassificationLevelLanguages()
    {
        return context.dbClassificationLevelLanguage;
    }

var currentUser = await userManager.GetUserAsync(User);
var defaultLanguageID = currentUser.DefaultLangauge;
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();
于 2019-08-26T17:54:16.073 回答
0

代码调用了 2 次相同的模型:

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 个表。

于 2019-08-27T03:04:06.190 回答