I've hit a strange problem With EF Core and I can't understand why ...
public class Startup
{
static Config Config;
public Startup(IConfiguration configuration)
{
Config = new Config();
configuration.Bind(Config);
}
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddScoped(ctx => ctx.GetService<IHttpContextAccessor>()?.HttpContext);
services.AddScoped(ctx => ctx.GetService<HttpContext>()?.Request);
services.AddAuthInfo();
services.AddSingleton(Config);
services.AddDbContext<MembershipDataContext>(options => options.UseSqlServer(Config.Connections["Membership"]));
services.AddDbContext<CoreDataContext>(options => options.UseSqlServer(Config.Connections["Core"]));
services.AddDbContext<B2BDataContext>(options => options.UseSqlServer(Config.Connections["B2B"]));
services.AddScoped<IMembershipDataContext, MembershipDataContext>();
services.AddScoped<ICoreDataContext, CoreDataContext>();
services.AddScoped<IB2BDataContext, B2BDataContext>();
...
... I extract custom auth information from each request and inject that in to my DbContexts.
Because of the init process I had to have a CTOR that accepts DbContextOptions so I simply added a second in the hope that it would call the right one ...
public EFDataContext(DbContextOptions options, IAuthInfo auth) : base(options) { AuthInfo = auth; }
public EFDataContext(DbContextOptions options) : base(options) { }
... At run time I see both CTOR's get hit several times inside a single request (not what I was expecting).
From other posts I note that many are saying I don't need the last three lines but removing them gives me exceptions telling me that other objects can no longer be constructed by DI.
So i'm confused ...
How do I get this working so that I can only construct 1 instance per request and only hit the CTOR with the most params when I do?