How to have unique entity framework connection strings in each user configuration file
I have a situation when some clients see the server by its local IP and some by global. So th开发者_如何学Goat for some of them IP is 10.0.0.4 and for some 94.44.224.132. I use ClickOnce for deployment and Entity Framework to generate the DB mapping. Now ive connection string setting in my user settings section and for each user i store his own one. After that for entity context's construction i do the following:
SomeEntities context = new SomeEntities(new EntityConnection("metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string=\"" + Properties.Settings.Default.ServerLocalConnectionString + "\""));
But there are some problems with Open/Close and Command execution after such approach. Is there some right way to store individual connection strings for every client and not overwrite them with deployment(ClickOnce is preferable)?
Found answer here.
EntityConnectionStringBuilder ecb = new EntityConnectionStringBuilder();
if (serverName=="Local")
{
ecb.ProviderConnectionString = Properties.Settings.Default.ServerLocalConnectionString;
}
else
{
ecb.ProviderConnectionString = Properties.Settings.Default.ServerConnectionString;
}
ecb.Metadata = "res://*/";
ecb.Provider = "System.Data.SqlClient";
SomeEntities context = new SomeEntities(new EntityConnection(ecb.ToString());
It seems this works. And now i can deploy the application and leave to the user the decision to which server does he want to connect and leave that configuration after updates, because its written in user's app.config.
精彩评论