-1

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();
}
Peter le Grand
  • 53
  • 1
  • 1
  • 8
  • Do you need the call to userManager.GetUserAsync to be Async? If you do it with the await, do you get the same error? – Casey Crookston Aug 26 '19 at 14:21
  • I did it with the await and then i get the error. The error actually happens on the TestForNull line of code. The question then is, What are the two operations that the error is talking about. Is one of them the previous line about the usermanager? Or are both operations in the TestForNull line? – Peter le Grand Aug 26 '19 at 14:25
  • Oops. Typo in my comment. If you do it withOUT the await, do you get the same error? – Casey Crookston Aug 26 '19 at 14:28
  • (side note... C# naming conventions would dictate that locally declared variables start with lower case. It makes them easier to spot when reused, and keeps them visually separate from class names and system variables. So `var CurrentUser = ` should really be `var currentUser = `. Same wit `DefaultLanguageID` – Casey Crookston Aug 26 '19 at 14:30
  • If I leave await out, then I get an error on the second line on DefaultLangauge (wiggly red line). Instead of GetUserAsync I didn't see an alternative that is not Async. Btw, I will try to improve on the conventions (still new to all). – Peter le Grand Aug 26 '19 at 14:37
  • You might find some help [here](https://stackoverflow.com/questions/48767910/entity-framework-core-a-second-operation-started-on-this-context-before-a-previ) – Josh Gust Aug 26 '19 at 16:14
  • What is the defination for `_classificationLevel` and `_classificationLevelLanguage`? Share us the full code. Are `GetAllClassificationLevels` and `GetAllClassificationLevelLanguages` in the same interfance? What is the purpose for sharing `IClassificationAndStatusRepository` which is not used in your current sharing code. – Edward Aug 27 '19 at 02:38

2 Answers2

0

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();
Wamiq Rehman
  • 566
  • 3
  • 11
0

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() and GetAllClassificationLevels() already include the foreign key tables. Thus both call the same 2 tables.

Peter le Grand
  • 53
  • 1
  • 1
  • 8