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 <> ...
await _context.Workers.ToListAsync().FirstOrDefault(w => w.ID == id);will work? Although it is a bad decision. - Bulson 2:21 pmEntityFrameworkand putEntityFramework Core- tym32167