c# - change OwinContext dbContext on runtime -


i need run-time change dbcontext request.getowincontext() use specific connectionstring it's not working.

i've dbcontex class accept default connectionstring web.config or specified 1 this:

public class applicationdbcontext : identitydbcontext<applicationuser> {     public applicationdbcontext()         : base("authentities", throwifv1schema: false)     {         configuration.proxycreationenabled = false;         configuration.lazyloadingenabled = false;     }      public applicationdbcontext(string sconnectionstring)         : base(sconnectionstring, throwifv1schema: false)     {         configuration.proxycreationenabled = false;         configuration.lazyloadingenabled = false;     }      public static applicationdbcontext create()     {         return new applicationdbcontext();     }      public static applicationdbcontext create(string sconnectionstring)     {         applicationdbcontext test = new applicationdbcontext(sconnectionstring);         return test;     }  } 

then when new user registering i've tried change owin's default dbcontext 1 new connectionstring this:

[allowanonymous] [route("create")] public async task<ihttpactionresult> createuser(createuserbindingmodel createusermodel) {      (...)       //get specific connectionstring      var connectionstring = cservice.getcompanyconnectionstring(createusermodel.company);       //generate new dbcontext      var appdbcontext = applicationdbcontext.create(connectionstring);       //get owincontext      var context = httpcontext.current.getowincontext();       //replace dbcontext inside owincontext      context.set<applicationdbcontext>("applicationdbcontext", appdbcontext);       (...) } 

until , including appdbcontext works expected on debuging, after setting context.set<applicationdbcontext>... context still using default one.

i've tried using var context = request.getowincontext(); , setting directly httpcontext.current.getowincontext().set<applicationdbcontext>("applicationdbcontext", appdbcontext); , request.getowincontext().set<applicationdbcontext>("applicationdbcontext", appdbcontext); same result.

the problem here first string trying use when setting, line

context.set<applicationdbcontext>("applicationdbcontext", appdbcontext); 

must changed to

context.set<applicationdbcontext>(appdbcontext); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -