efcore icon indicating copy to clipboard operation
efcore copied to clipboard

A question about the use of AddDbContextPool

Open henrydingchina opened this issue 1 year ago • 0 comments

Ask a question

  • When I register my dbcontext with AddDbContextPool, my dbcontext can't add additional service instances to the constructor
  • There was an error while starting the service

InvalidOperationException: The DbContext of type 'GBDbContext' cannot be pooled because it does not have a public constructor accepting a single parameter of type DbContextOptions or has more than one constructor

DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions 1[VideoFusionService.Extensions.GBDbContext]) on type 'DbContextPool1.

Include your code

 services.AddDbContextPool<TDbContext>(db =>
            {
                if (sqlDb.Equals(Constants.MYSQL))
                {
                    UseMySql(connectionString, db);
                }
                else
                {
                    UseDm(connectionString, db);
                }
            });

 private readonly IMediator m_mediator;
        /// <inheritdoc />
        public GBDbContext(DbContextOptions options, IServiceScopeFactory m_serviceScopeFactory)
            :base(options)
        {
            m_mediator = m_serviceScopeFactory.CreateScope().ServiceProvider.GetService<IMediator>();
        }

Include stack traces

 InvalidOperationException: The DbContext of type 'GBDbContext' cannot be pooled because it does not have a public constructor accepting a single parameter of type DbContextOptions or has more than one constructor

DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions 1[VideoFusionService.Extensions.GBDbContext]) on type 'DbContextPool1.

Include provider and version information

EF Core version: 5.0.17 Database provider: (e.g. Microsoft.EntityFrameworkCore.MySql) Target framework: (e.g. .NET 6.0) Operating system: IDE: (e.g. Visual Studio 2019 4)

henrydingchina avatar May 20 '24 08:05 henrydingchina