c# - Entity Framework DbContext dynamic instatiation with custom Connection String -
migrating objectcontext dbcontext code-generation, realized context class generated (which inherits dbcontext) has no constructor receives connectionstring neither entityconnection (like objectcontext child class had).
this problem in application since need instantiate context dinamically concrete type, using runtime generated connection string.
any ideas?
on class inherits dbcontext, should able specify base constructor takes connection sting:
public class mydbcontext : dbcontext { public mydbcontext(string connstring) : base(connstring) { } }
you have use sqlconnection builder though:
sqlconnectionstringbuilder connbuilder= new sqlconnectionstringbuilder(dbconnstring);
and use in constructor:
mydbcontext dbcontext = new mydbcontext(connbuilder.tostring());
Comments
Post a Comment