Skip to content
loading...

re: Wiring up Ninject with ASP.NET Core 2.0 VIEW POST

FULL DISCUSSION
 

All of this works fine except for a couple of tweaks needed to use a custom DbContext. I have ApplicationDbContext, which inherits from IdentityDbContext.

Within Startup.cs, I added an IConfiguration parameter to the Configure and RegisterApplicationComponents methods (one feeds the other of course), then within RegisterApplicationComponents I added:

kernel.Bind<IConfiguration>().ToConstant(configurationVariable);

My API is running in Azure and the IConfiguration gets injected at runtime. Then, within the ApplicationDbContext class, I had to add the following constructor:

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IConfiguration configurationVariable) : base(options)

Within the constructor I call configurationVariable.GetConnectionString("MyConnectionStringName") and assign it to a private string variable "_connectionStringVariable" within the ApplicationDbContextClass. Then added the following override:

protected override void OnModelCreating(ModelBuilder builder)

...which contains a couple of bits specific to my application but most importantly calls base.OnModelCreating(builder). I then added the following override:

protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
builder.UseSqlServer(_connectionStringVariable);
_connectionString = null;
base.OnConfiguring(builder);
}

The ApplicationDbContext can then be used by my internal services.

code of conduct - report abuse