I had a problem with asynchronous database queries when using the Entity Framework. When I try to get some value from the database, for example:

[Authorize] public async Task<IActionResult> Edit(int? id) { if (id != null) { Worker worker = await _context.Workers.FirstOrDefaultAsync(w => w.ID == id); if (worker != null) { return View(worker); } } return NotFound(); } 

Then the following exception appears:

 The provider for the source IQueryable doesn't implement IDbAsyncQueryProvider. Only providers that implement IDbAsyncQueryProvider can be used for Entity Framework asynchronous operations. 

Here is the access context class for the database:

 public class EnterChatContext : DbContext { public EnterChatContext(DbContextOptions<EnterChatContext> options) : base(options) { } public DbSet<User> Users { get; set; } public DbSet<TopicMessage> TopicMessages { get; set; } public DbSet<Topic> Topics { get; set; } public DbSet<Note> Notes { get; set; } public DbSet<GroupChatMessage> GroupChatMessages { get; set; } public DbSet<File> Files { get; set; } public DbSet<Company> Companies { get; set; } public DbSet<Worker> Workers { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys())) { relationship.DeleteBehavior = DeleteBehavior.Restrict; } modelBuilder.Entity<User>().ToTable("User"); modelBuilder.Entity<TopicMessage>().ToTable("TopicMessage"); modelBuilder.Entity<Topic>().ToTable("Topic"); modelBuilder.Entity<Note>().ToTable("Note"); modelBuilder.Entity<GroupChatMessage>().ToTable("GroupChatMessage"); modelBuilder.Entity<File>().ToTable("File"); modelBuilder.Entity<Company>().ToTable("Company"); modelBuilder.Entity<Worker>().ToTable("Worker"); } } 

Here is the Worker model class.

 public class Worker { public int ID { get; set; } public int CompanyID { get; set; } [Required(ErrorMessage = "ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, Π²Π²Π΅Π΄ΠΈΡ‚Π΅ имя Ρ€Π°Π±ΠΎΡ‚Π½ΠΈΠΊΠ°!")] public string FirstName { get; set; } [Required(ErrorMessage = "ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, Π²Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ„Π°ΠΌΠΈΠ»ΠΈΡŽ Ρ€Π°Π±ΠΎΡ‚Π½ΠΈΠΊΠ°!")] public string SecondName { get; set; } public bool Status { get; set; } public string StringStatus { get { if (Status == true) return "Π”Π°"; else return "НСт"; } } [Required(ErrorMessage = "ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, Π²Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΏΡ€ΠΈΠ³Π»Π°ΡΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ ΠΊΠΎΠ΄ для Ρ€Π°Π±ΠΎΡ‚Π½ΠΈΠΊΠ°!")] public int? InviteCode { get; set; } public Company Company { get; set; } } 

I can not find I can find information on this exception. I understand the problem with the context class, you need to use something else instead of DbSet <> ...

  • And so await _context.Workers.ToListAsync().FirstOrDefault(w => w.ID == id); will work? Although it is a bad decision. - Bulson 2:21 pm
  • What database is used? - tym32167
  • In my asp.net core 2 application I also use DbSet and everything is fine, but I have mysql - tym32167 dd
  • @ tym32167 localdb - YungBlade
  • one
    So remove EntityFramework and put EntityFramework Core - tym32167

2 answers 2

I can not exactly answer, but everywhere they recommend installing the NuGet package
Link to the English form with the same question

  • set, did not help - YungBlade

The problem was the conflict between EntityFramework and EntityFrameworkCore. Asynchronous methods simply did not refer to the implementation that is needed. I removed the EntityFramework from the project and used the directive

 using Microsoft.EntityFrameworkCore;