I have many test classes, and each has dozens of tests. I want to isolate tests, so instead of a mega context MyDbContext
, I use MyDbContextToTestFoo
, MyDbContextToTestBar
, MyDbContextToTestBaz
, etc. So I have MANY DbContext
subclasses.
In my unit tests with EF Core 5 I'm running into the ManyServiceProvidersCreatedWarning
. They work individually, but many fail when run as a group:
System.InvalidOperationException : An error was generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.ManyServiceProvidersCreatedWarning': More than twenty 'IServiceProvider' instances have been created for internal use by Entity Framework. This is commonly caused by injection of a new singleton service instance into every DbContext instance. For example, calling 'UseLoggerFactory' passing in a new instance each time--see https://go.microsoft.com/fwlink/?linkid=869049 for more details. This may lead to performance issues, consider reviewing calls on 'DbContextOptionsBuilder' that may require new service providers to be built. This exception can be suppressed or logged by passing event ID 'CoreEventId.ManyServiceProvidersCreatedWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
I don't do anything weird with DbContextOptionsBuilder
as that error suggests. I don't know how to diagnose "...that may require new service providers to be built". In most tests I create a context normally: new DbContextOptionsBuilder<TContext>().UseSqlite("DataSource=:memory:")
where TContext
is one of the context types I mentioned above.
I've read many issues on the repo, and discovered that EF does heavy caching of all sorts of things, but docs on that topic don't exist. The recommendation is to "find what causes so many service providers to be cached", but I don't know what to look for.
There are two workarounds:
builder.EnableServiceProviderCaching(false)
which is apparently very bad for perfbuilder.ConfigureWarnings(x => x.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning))
which ignores the problem
I assume that "service provider" means EF's internal IoC container.
What I want to know is: does the fact that I have many DbContext
types (and thus IModel
types), affect service provider caching? Are the two related? (I know EF caches an IModel
for every DbContext
, does it also cache a service provider for each one?)