c# - ASP.NET vNext EF7 dbContext issues -
i starting vnext project, , i'm having issues kicking off ground. have added table applicationdbcontext
class, , created table in db (which in case in azure). however, can't seem correctly instantiate dbcontext
use in controllers.
in experience previous asp.net ef projects, instantiate applicationdbcontext class without passing parameters, in case of vnext however, seems expect number of things (iserviceprovider
, , ioptionsaccessor<dbcontextoptions>
). have tried creating parameter-less constructor, app breaks due not knowing connection strings use. code below -- see in onconfiguring(dbcontextoptions options)
override, force connection string in via dbcontextoptions
, that's not ideal, , feel i'm not understanding 2 iserviceprovider
, , ioptionsaccessor parameters need come from.
thanks help!
namespace project.models { // add profile data application users adding properties applicationuser class public class applicationuser : identityuser { public string companyname { get; set; } } public class applicationdbcontext : identitydbcontext<applicationuser> { private static bool _created = false; public dbset<business> businesses { get; set; } public applicationdbcontext() : base() { if (!_created) { database.ensurecreated(); _created = true; } } protected override void onconfiguring(dbcontextoptions options) { var configuration = new configuration(); configuration.addjsonfile("config.json"); configuration.addenvironmentvariables(); options.usesqlserver(configuration.get("data:defaultconnection:connectionstring")); } public applicationdbcontext(iserviceprovider serviceprovider, ioptionsaccessor<dbcontextoptions> optionsaccessor) : base(serviceprovider, optionsaccessor.options) { // create database , schema if doesn't exist // temporary workaround create database until entity framework database migrations // supported in asp.net vnext if (!_created) { database.ensurecreated(); _created = true; } } } }
iserviveprovider , ioptionaccessor injected dependency injection asp.net core di has limitation, cannot have more 1 constructor.
read this: http://blogs.msdn.com/b/webdev/archive/2014/06/17/dependency-injection-in-asp-net-vnext.aspx
Comments
Post a Comment