DbContext with Async queries
Hello,
EF: 6.1.3
When I use any async methods such as .ToListAsync(), .FirstOrDefaultAsync() etc, my query execution freezes and it never returns anything. If I use synchronous methods such ash .ToList() on same DbContext, then I get the results which is expected. I am not able to understand what is the issue with async methods. Please help.
Regards, Romil
Are you using the await keyword?
var engineers = await _db.Engineers
.Where(x => x.Forename == forename && x.Surname == surname)
.ToListAsync();
Yes ! I am using async and await both the keywords. But not sure why the execution stuck while calling .ToListAsync() method.
I am using Unit Of Work + Repository Pattern where my dbContext is shared to all repositories from UoW.
Is your project using .Net Framework 4.5 or later? Async was only introduced in 4.5 onwards.
@romil-zealous does your execution get stuck the same way if you replace the awaiting statement with this?
await Task.Delay(3000);
@sjh37 I am using .NET Framework 4.6.2
EF Team Triage: This issue is lacking enough information for us to be able to effectively triage it. In particular, it is missing the following information requested in the new issue template. Can you please provide this information?
Steps to reproduce
Ideally include a complete code listing that we can run to reproduce the issue. Alternatively, you can provide a project/solution that we can run.
BTW we're not just doing this to be mean :smile:... we get a lot traffic on this project and it takes time to attempt to reproduce an issue based on fragments of information. In addition, our attempt is often unsuccessful as the exact conditions required to hit the issue are often not explicitly included in the code provided. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we ask that folks give us a self-contained way to reproduce an issue.
For a guide on submitting good bug reports, read Painless Bug Tracking.
BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.
@romil-zealous does this help?: https://stackoverflow.com/a/22017717/492
EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.
BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.
Hi, I have the same issue with I use mysql provider. ToListAsync blocked in Console Application and never return result.
MySql EF 8.0.12 EF 6.2.0 .Net Framework 4.5.2 Console Application
Small demo app.
class Program
{
static void Main(string[] args)
{
IMigrator migrator = new Migrator();
// Fire and forget
migrator.MigrateAsync();
Console.ReadLine();
}
public interface IMigrator
{
Task MigrateAsync();
}
public class Migrator : IMigrator
{
public async Task MigrateAsync()
{
using (var context = new DbContext(connection))
{
while (true)
{
// Never return back. ConfigureAwait not affect. Task.Delay works for me
var items = await context.Users.ToListAsync().ConfigureAwait(true);
if (items.Count == 0)
{
return;
}
}
}
}
}
}