Eror the DbContext of type cannot be pooled in Asp.Net Core

Dung Do Tien Feb 25 2022 56

Hi, I have a project, using Asp.Net Core 3.1, FE Core 3.1, MySQL database. Below is my DBContext:

My DBContext

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

// Code scaffolded by EF Core assumes nullable reference types (NRTs) are not used or disabled.
// If you have enabled NRTs for your project, then un-comment the following line:
// #nullable disable

namespace Test.GoogleNotification.Dal.Entities
{
    public partial class GoogleNotificationContext : DbContext
    {
        public GoogleNotificationContext()
        {
        }

        public GoogleNotificationContext(DbContextOptions<GoogleNotificationContext> options)
            : base(options)
        {
        }

        public virtual DbSet<NotifyHistory> Notifyhistory { get; set; }
        public virtual DbSet<User> User { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseMySQL("server=127.0.0.1;uid=root;pwd=sa@12345;database=googlenotification");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<NotifyHistory>(entity =>
            {
                entity.ToTable("notifyhistory");

                entity.HasIndex(e => e.NotifyHistoryId)
                    .HasName("NotifyHistoryId_UNIQUE")
                    .IsUnique();

                entity.Property(e => e.Link).HasMaxLength(150);

                entity.Property(e => e.Message).HasMaxLength(500);

                entity.Property(e => e.Status).HasColumnType("bit(1)");

                entity.Property(e => e.Title).HasMaxLength(100);
            });

            modelBuilder.Entity<User>(entity =>
            {
                entity.ToTable("user");

                entity.HasIndex(e => e.UserId)
                    .HasName("UserId_UNIQUE")
                    .IsUnique();

                entity.Property(e => e.CreatedDate).HasDefaultValueSql("CURRENT_TIMESTAMP");

                entity.Property(e => e.IpAddress).HasMaxLength(100);

                entity.Property(e => e.SubscribeToken).HasMaxLength(500);
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}

Inject Dbcontext in Startup.cs 

// Add EF connection
services.AddDbContextPool<GoogleNotificationContext>(x=> {
    x.UseMySQL(Configuration.GetConnectionString("GoogleNotifyConnectionString"));
});

services.AddTransient<GoogleNotificationContext>();

But when run the project I got an exception throw System.InvalidOperationException: 'The DbContext of type 'GoogleNotificationContext' cannot be pooled.

System.InvalidOperationException: 'The DbContext of type 'GoogleNotificationContext' cannot be pooled 
because it does not have a single public constructor accepting a single parameter of type DbContextOptions.'

How can I solve it?

Have 2 answer(s) found.
  • I

    ImamShafie Jubaiha Feb 25 2022

    I got the same issue, You can add more UseInternalServiceProvider() method when you add DB context pool as below:

    services.AddDbContextPool<GoogleNotificationContext>(x => {
        x.UseSqlServer("Your Connection String");
        x.UseInternalServiceProvider(provider);   /// Add more this line
    });

    It really worked for me, hope it also works for you.

  • D

    Duc Viet Phung Feb 25 2022

    You can remove default constructor in GoogleNotificationContext.cs 

    Remove some lines below:

    public GoogleNotificationContext()
    {
    }

    I hope it works for you!

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close