Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

asp.net - Based on a Linq join I get the error A second operation started on this context before a previous operation completed

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();

While running it, I get the error the error

A second operation started on this context before a previous operation completed.

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.

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()

Is this the reason for the error?

The methods behind these are:

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

and

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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You must have to await on all the succeeding steps in order to properly use the aync/await structure.

You can use CountAsync() in your case but async count is only an extension of IQueryable ,so you might have to change you method return type IQueryable which is more faster and safer

    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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...