In my configuration files I have a connection string used by a legacy part of the app (using Datasets) and another string for Entity Framework:
<connectionStrings>
<add name="Database" connectionString="Server=..." />
<add name="Entities" connectionString="metadata=.....connection string='Server=..." />
</connectionStrings>
This means the server name, database name etc. are specified twice. I'd like to tell the EF connection string to reuse the first string - is this possible?
If you want to change the connection string go to the app. config and remove all the connection strings. Now go to the edmx, right click on the designer surface, select Update model from database, choose the connection string from the dropdown, Click next, Add or Refresh (select what you want) and finish.
If you save your connection string in the udl file, the user can change the connection via an interface by simply double clicking that file. You can set your connection string in the app to point to the udl file. You can also launch the udl interface programmatically if you want.
A connection string contains initialization information that is passed as a parameter from a data provider to a data source. The syntax depends on the data provider, and the connection string is parsed during the attempt to open a connection.
config based connectionstring as seems is unsafe, because one can read it. But think about it, if a person can read your web. config, means he can edit any file on your server anyways as he probably already hack or gain access to file.
I know this post is a bit old, but I figure this will help someone out there:
You can use the EntityConnectionStringBuilder to build your EF connection from your existing connection string. This is a sample I'm using in my own code:
public static string GetEntityFrameworkConnectionString(string clientConnectionString)
{
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "System.Data.SqlClient";
entityBuilder.ProviderConnectionString = clientConnectionString;
entityBuilder.Metadata = "res://*/Entities.UBTEntities.csdl|res://*/Entities.UBTEntities.ssdl|res://*/Entities.UBTEntities.msl";
return entityBuilder.ToString();
}
So when you instantiate your EF provider, just pass in the string returned from the method above into the constructor.
Hope this helps.
I think a better approach would be to refactor the application to use just one connection string rather than trying to reference one from the other in your configuration file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With