1

I have this auto-generated connection string in my app.config file. This was produced by making a database-first model in Entity Framework:

<add name="HalloweenContext" connectionString="metadata=res://*/HalloweenDbModel.csdl|
                                                    res://*/HalloweenDbModel.ssdl|
                                                    res://*/HalloweenDbModel.msl;
                         provider=System.Data.SqlClient;
                         provider connection string=&quot;
                         data source=localhost\sqlexpress;
                         initial catalog=Halloween;
                         integrated security=True;
                         MultipleActiveResultSets=True;
                         App=EntityFramework&quot;"
 providerName="System.Data.EntityClient" />

I've been instructed to take that connection string and put it in the application settings (image is to illustrate the application settings page):

enter image description here

I've taken this section and cut/pasted it into a settings variable called "HalloweenContext" and also changed the &quote; to a single-quote:

"metadata=res://*/HalloweenDbModel.csdl|
          res://*/HalloweenDbModel.ssdl|
          res://*/HalloweenDbModel.msl;
          provider=System.Data.SqlClient;
          provider connection string=';
          data source=localhost\sqlexpress;
          initial catalog=Halloween;
          integrated security=True;
          MultipleActiveResultSets=True;
          App=EntityFramework';"
 providerName="System.Data.EntityClient"

However, when I paste this above selection into the application settings variable for HallowContext, I get the following error message:

enter image description here

I've checked the connection string and rechecked it, but I can't figure out what's wrong with it. I've removed quotes, added quotes, etc. but nothing seems to work for me.

I'm wondering if, perhaps, since this is an EF connection string, that I'm putting it into the application settings incorrectly?

UPDATE: After using the new data connection string outlined in the answer below, I am now getting a new error:

The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection...

So, I'm thinking that I may not be able to use this context in lieu of my db-first context. But, I also can't seem to use my db-first context as it is, so I'm at a standstill. I'll keep looking for a solution, and I do appreciate the help given so far.

As an added note, I'll restate that the original connection string works fine and is in the app.config file. However, I've been instructed to add it to the application settings. Personally, I feel that if it works in the app.config file, then that should be enough, but we've been instructed to use the connection string by adding it to the application settings and using it like connectionString = Settings.Default.HaloweenContext. The ultimate goal is to have many connection strings set up in the application settings that we can use.

SIDE NOTE: I'm of the belief that using multiple named connection strings in the app.config file is the way to go. Is there any advantage to adding those connection strings as application settings instead of just manually adding them to the app.config file?

4
  • 1
    I never understand why people want to defeat the built-in configuration features of .NET and force it to use a system that is more error prone and subject to compilation problems. ConnectionStrings section is the proper place for connection strings in .NET, using other configuration areas is abnormal. Typically, this stems from a misunderstanding or something, or only understanding how to do things a certain way (the "when you only have a hammer, everything looks like a properties" syndrome. Commented Aug 18, 2014 at 16:11
  • I agree. I have all the connection strings in their proper place in the app.config, and it works great! Unfortunately, as stated above, the boss wants them in the Application Settings. Which, by the way, adds them to the app.config file anyway! Grrr!
    – Kevin
    Commented Aug 18, 2014 at 16:50
  • Have you asked the boss why he wants this? Whenever my boss asks for something unreasonable, I usually find asking him to explain the reason behind his request helps me to explain why his request may not be the best choice. Sometimes the bosses request these things because they want integration with some other tool, or have other business reasons, but there are other ways to achieve those end results. Commented Aug 18, 2014 at 16:58
  • VS also doesn't natively have a way to use a different connection string per build configuration. (Ya have to do a post-build thing to do the transform (which is normally done on release)) Commented Apr 13, 2015 at 19:56

1 Answer 1

4

That is not an ADO.NET connection string, that looks like an EF string

Here is the conn string you want:

"data source=localhost\sqlexpress; initial catalog=Halloween; integrated security=True;MultipleActiveResultSets=True;"
7
  • Yes, as I said above, this connection string was generated when the model was created from a database. Are you implying that I cannot use this connection string in the application settings because it is an entity framework connection string?
    – Kevin
    Commented Aug 7, 2014 at 13:58
  • yes, i've updated my answer with the conn string you should use.
    – T McKeown
    Commented Aug 7, 2014 at 14:01
  • After trying your solution, it got me past the error "starting at index 129...", but I'm getting another error, which I've updated in my original question above.
    – Kevin
    Commented Aug 7, 2014 at 14:28
  • are you trying to access EF classes without have the EF connection string?
    – T McKeown
    Commented Aug 7, 2014 at 15:24
  • Not at all. The EF connection string, the one I pasted in above, is the string generated when I did the db-first model creation. It's in the app.config file and works like a charm. The only problem is that the boss wants all configuration strings set up in the application settings page as variables. Apparently, I can't just past in the db-first EF connection string from the app.config file.
    – Kevin
    Commented Aug 7, 2014 at 15:42

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.