130

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.

5 Answers 5

203

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.

8
  • 17
    Excellent. This is exactly the answer I was looking for. Just to be safe I commented out the existing string (rather than delete it), saved the app.config changes, right-clicked the designer and chose Update Model From Database. The wizard then let me include the sensitive info (uid & pwd) in the connection string. Thanks! Commented Mar 14, 2011 at 15:41
  • 5
    If you're doing model-first then right-click the designer and click 'Generate Database from Model' instead Commented Jun 12, 2012 at 9:03
  • 2
    This worked for me as well, except I had to update the App.config file, then restart VS.NET 2012. It was not detecting that the App.config file changed. Commented May 28, 2013 at 2:47
  • 2
    I had to explicitly call save on the app.config file for the designer to recognise the connection string had been deleted.
    – Rossco
    Commented May 9, 2014 at 2:37
  • 1
    An alternative to restarting VS is to 1. comment-out the connection string in app.config 2. rebuild the project that contains the .edmx 3. right-click the .edmx design surface and Update Model From Database... which should bring up the Connection String wizard.
    – RIanGillis
    Commented Aug 3, 2016 at 18:43
18

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.

2
  • +1 for reminding that : if they put it in a configuration file, it means that they want you to be able to change it. I was mainly concerned about the fact that my changes could be overriden
    – tobiak777
    Commented Jun 22, 2015 at 11:56
  • You should go and change the connection string in the app config of the project edmx belongs to. Not the config of top level application (which is used when it runs). o_0
    – akava
    Commented Jul 30, 2015 at 16:05
13

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.

3
  • I am having a similar issue but when I comment out the connection string, update the model, choose a new connection string, my context.cs class within the model gets emptied out. Not sure why its doing this unless its just failing to generate the context class?
    – bitshift
    Commented Nov 9, 2016 at 21:01
  • 1
    It should not do that. May be your T4 templating tool is corrupted.
    – DanKodi
    Commented Nov 10, 2016 at 0:08
  • I agree, it "shouldnt". However, Ive lost count of the number of times when making more than a few non-trivial changes to the underlying schema, that I had eventually to simply blow away the model and create a new one. Maybe with EF7 and its removing of the edmx file might help things. Either way, thats I what I did this time as well, just highlighted everything in the model, hit delete and brought those items back in.
    – bitshift
    Commented Nov 10, 2016 at 13:21
4

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!!
1

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.

2
  • Yeap, this did it.
    – Luis Deras
    Commented Nov 17, 2016 at 18:01
  • What does this mean? Schema is an xml element. It cannot be changed to "your required schema" Commented Jan 16, 2023 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.