Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

New Post: set db parameters of db context at runtime

$
0
0
hi all,
I created a new database model .edmx inside visual studio 2013 using ef 6 beta 1 importing from an existing database.
The dbcontext that is created when the model is saved contains the reference to the name of the connection string in the web config,
    public sg_cuscinettoEntities()
        : base("name=sg_cuscinettoEntities")
    {
    }
that contains both the metadata reference that the connection data.
<add name="sg_cuscinettoEntities" connectionString="metadata=res://*/Model.SgWsModel.csdl|res://*/Model.SgWsModel.ssdl|res://*/Model.SgWsModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=...;initial catalog=...;persist security info=True;user id=...;password=...;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
Now, I need to use this model with different databases, that I know use this model, but I have to change the connection parameters in Runtime, and I would like to use something like the old SlqConnectionStringBuilder.
As first, using the DbContext created from the model I'm not able to change the connection string, because the only constructor is without parameters, and the connection string is hardwired.
Adding to the .tt file a second costructor I suppose I can expose this, but I have another problem: I have to try, but if I remember well,if I add to the constructor a standard connection string, the db context is able to connect, but loose all the metadata of the model, and it works just with connection string wrote in the web.config.
How can I solve? adding the second constructor, copy in the program the conn string from the web.config, replace the db parameters in the string and use it as a connection strign may it work?
thanks,
luca

Viewing all articles
Browse latest Browse all 10318

Trending Articles