Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Entity Framework connection strings reuse an existing connection string?

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?

like image 821
Paul Stovell Avatar asked Mar 26 '09 03:03

Paul Stovell


People also ask

How do I change the connection string in Entity Framework?

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.

How do I change the connection string after deployment?

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.

What is connection string in Entity Framework?

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.

Is it safe to store connection string in web config?

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.


2 Answers

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.

like image 114
Sebastian Gvirtzman Avatar answered Sep 24 '22 13:09

Sebastian Gvirtzman


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.

like image 34
Andrew Hare Avatar answered Sep 22 '22 13:09

Andrew Hare