c# - How can I get IHostingEnvironment from DbContext in ASP.NET Core -
just testing, don't want dependency injection startup.cs file. how can ihostingenvironment ef core dbcontext.
i take new asp.net core project empty template. have created dbcontext below. want user environment.contentrootpath instead of directory.getcurrentdirectory(). don't want injection startup.cs.
public class mydbcontext: dbcontext { public iconfigurationroot configuration { get; set; } protected override void onconfiguring(dbcontextoptionsbuilder optionsbuilder) { var builder = new configurationbuilder() .setbasepath(path.combine(directory.getcurrentdirectory(), "..\\..\\..")) .addjsonfile("appsettings.json", optional: true, reloadonchange: true); configuration = builder.build(); optionsbuilder.usesqlserver(configuration.getconnectionstring("sqlcn"));// @"server=(localdb)\mssqllocaldb;database=mydatabase;trusted_connection=true;"); } public dbset<teacher> teachers { get; set; } public dbset<lesson> lessons { get; set; } }
if add ihostingenvironment in dbcontext constructor below,
public class mydbcontext: dbcontext { private readonly ihostingenvironment env; public mydbcontext(ihostingenvironment env) : base() { this.env = env; } public iconfigurationroot configuration { get; set; } protected override void onconfiguring(dbcontextoptionsbuilder optionsbuilder) { var builder = new configurationbuilder() .setbasepath(this.env.contentrootpath) .addjsonfile("appsettings.json", optional: true, reloadonchange: true); configuration = builder.build(); optionsbuilder.usesqlserver(configuration.getconnectionstring("sqlcn"));// @"server=(localdb)\mssqllocaldb;database=mydatabase;trusted_connection=true;"); } public dbset<teacher> teachers { get; set; } public dbset<lesson> lessons { get; set; } }
i got following error when add-migration package-manager console.
pm> add-migration initmydbcontext
no parameterless constructor found on 'mydbcontext'. either add parameterless constructor 'mydbcontext' or add implementation of 'idbcontextfactory' in same assembly 'mydbcontext'.
you should able inject ihostingenvironment
directly dbcontext's constructor.
public class mydbcontext: dbcontext { private readonly ihostingenvironment env; public mydbcontext(ihostingenvironment env) : base() { this.env = env; } public iconfigurationroot configuration { get; set; } protected override void onconfiguring(dbcontextoptionsbuilder optionsbuilder) { var builder = new configurationbuilder() .setbasepath(env.contentrootpath) .addjsonfile("appsettings.json", optional: true, reloadonchange: true); configuration = builder.build(); optionsbuilder.usesqlserver(configuration.getconnectionstring("sqlcn"));// @"server=(localdb)\mssqllocaldb;database=mydatabase;trusted_connection=true;"); } public dbset<teacher> teachers { get; set; } public dbset<lesson> lessons { get; set; } }
the framework aware of interface.
you add context usual. minus configuration, indicated don't want in startup
services.adddbcontext<mydbcontext>();
when initializing context framework should inject implementation of ihostingenvironment
context based on presence of constructor argument.
Comments
Post a Comment