I have async issue with my below query. I have singleton context and i am trying to execute below query:
var query = await (from parent in Context.ParentTable
join child in Context.ChildTable
on parent.ID equals child.ID
into allResult
from ResultValue in allResult.DefaultIfEmpty()
where ResultValue.TenantId == tenantId
select new Result
{
Code = parent.Code,
Type = parent.Type,
ID = ResultValue == null ? 0 : ResultValue.Id
}).ToListAsync();
My singleton context looks like this:
public class BaseRepository
{
private readonly IConfigurationContextFactory configurationContextFactory;
private IConfigurationContext context;
protected IConfigurationContext Context
{
get
{
return context ?? (context = configurationContextFactory.Context);
}
}
public BaseRepository(IConfigurationContextFactory configurationContextFactory)
{
this.configurationContextFactory = configurationContextFactory;
}
}
The configuration context factory returns Context like this:
private ConfigurationContext Get()
{
logger.WriteEntrySync(LogLevel.Information,
null != context ? "Config Context: Using existing context." : "Config Context: Wiil create new context.");
return context ?? (context = new ConfigurationContext(connectionString));
}
In this i get intermittent issue with following error:
A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.