Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a separate file to maintain the connection string for entity framework

I have my connection string currently in my web.config file.

Is it possible to place it in a separate file and point entity framework to it.

like image 654
Mortalus Avatar asked Mar 01 '13 08:03

Mortalus


People also ask

What is the best way to store the connection string?

Connection strings can be stored as key/value pairs in the connectionStrings section of the configuration element of an application configuration file.

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.

Which file you should write for the connection string?

ANSWER: In Web. config file.

Is it safe to store connection string in web config?

The connection strings are mostly stored in web. config. It means that connection specific information such as database name, username, and password are stored as a clear text in a file. This is definitely a security concern for your Production servers.


2 Answers

I found the answer here Separate ConnectionStrings and mailSettings from web.config? Possible?:

<configuration>
    <connectionStrings configSource="connections.config"/> 
</configuration>

With file connections.config containing

<connectionStrings>
   <add name="name" connectionString="conn_string" providerName="System.Data.SqlClient" />
   <add name="name2" connectionString="conn_string2" providerName="System.Data.SqlClient" />
</connectionStrings>
like image 124
Mortalus Avatar answered Oct 02 '22 06:10

Mortalus


In case anyone stumbles upon this question. You can put the connection strings in a separate config file using configSource but DONT expect the EF designer to work happily with it.

Every time to go to edit the edmx and 'Update from Database' it will ask for a new connection string and then always want to save it back to the web.config. Not ideal and for me not workable. This is the case in EF6 and previous.

like image 31
Zac Avatar answered Oct 02 '22 07:10

Zac