How should I edit an Entity Framework connection string?

Entity FrameworkEntity Framework-4Connection StringApp Config

Entity Framework Problem Overview


I recently had to edit my app.config file to change the connection string for an Entity Framework data model (.edmx file). But I'd like to know: Is there a way to edit the EF connection string using the designer?

The original connection string is auto-generated by the Entity Data Model wizard. The connection string then cannot be changed - it remains disabled in the properties window of the .edmx designer. I like to avoid editing XML by hand if I can (in app.config), so I was wondering if there is a way to get back into the original wizard screen to make connection string changes and let the tool edit the app.config file for me.

Entity Framework Solutions


Solution 1 - Entity Framework

If you remove the connection string from the app.config file, re-running the entity Data Model wizard will guide you to build a new connection.

Solution 2 - Entity Framework

No, you can't edit the connection string in the designer. The connection string is not part of the EDMX file it is just referenced value from the configuration file and probably because of that it is just readonly in the properties window.

Modifying configuration file is common task because you sometimes wants to make change without rebuilding the application. That is the reason why configuration files exist.

Solution 3 - Entity Framework

You normally define your connection strings in Web.config. After generating the edmx the connection string will get stored in the App.Config. 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.

In the output window it will show something like this,

Generated model file: UpostDataModel.edmx. Loading metadata from the database took 00:00:00.4258157. Generating the model took 00:00:01.5623765. Added the connection string to the App.Config file.

Solution 4 - Entity Framework

Open .edmx file any text editor change the Schema="your required schema" and also open the app.config/web.config, change the user id and password from the connection string. you are done.

Solution 5 - Entity Framework

Follow the next steps:

  1. Open the app.config and comment on the connection string (save file)
  2. Open the edmx (go to properties, the connection string should be blank), close the edmx file again
  3. Open the app.config and uncomment the connection string (save file)
  4. Open the edmx, go to properties, you should see the connection string uptated!!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeveloperDanView Question on Stackoverflow
Solution 1 - Entity FrameworkFabian NicollierView Answer on Stackoverflow
Solution 2 - Entity FrameworkLadislav MrnkaView Answer on Stackoverflow
Solution 3 - Entity FrameworkDanKodiView Answer on Stackoverflow
Solution 4 - Entity Frameworkbijon75View Answer on Stackoverflow
Solution 5 - Entity FrameworkCesar Alvarado DiazView Answer on Stackoverflow