Entity Framework - The DbContext of type 'SampleContext' cannot be pooled because it does not have a single public constructor accepting a single parameter of type DbContextOptions.
I was looking at some ways to improve the performance of one of our applications. One of the things I wanted to try was to active DbContext pooling(https://bartwullems.blogspot.com/2018/03/entity-framework-core-20dbcontext.html).
Unfortunately after adding the following lines:
services.AddDbContextPool<BloggingContext>( | |
options => options.UseSqlServer(connectionString) | |
); |
the application started to fail with this exception message:
System.InvalidOperationException: The DbContext of type 'TargetSUContext' cannot be pooled because it does not have a single public constructor accepting a single parameter of type DbContextOptions.
at Microsoft.EntityFrameworkCore.Internal.DbContextPool`1..ctor(DbContextOptions options)
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__5`2.<AddDbContextPool>b__5_1(IServiceProvider sp)
at lambda_method(Closure , IBuildSession , IContext )
Let’s take a look at my DbContext and indeed I had multiple constructors:
public class SampleContext:DbContext | |
{ | |
public SampleContext() | |
{ | |
} | |
public SampleContext(string connectionString) | |
{ | |
_connectionString = connectionString; | |
} | |
public SampleContext(DbContextOptions<SampleContext> options) : base(options) | |
{ | |
} | |
//Removed other code | |
} |
I don’t understand why Entity Framework cannot handle this, but I removed the other constructors to make EF happy…