No connection string named could be found in the application config file
Asked Answered
B

10

19

I'm using EF and generated .EDMX from it but then I only wanted it to be used for automated generation of Class Files.

I then used the Class Files to create a Entity Model and then created a DB Context and then Repository. I'm calling a WebApi (which is in a separate project but same solution) to access the repository to GET data. While I run the WebApi, I'm getting the error,

{"No connection string named 'DBEntities' could be found in the application config file."}

But within my DAL, I have a webConfig and that has the following entry so I'm not quite sure what has gone wrong,

add name="DBEntities" connectionString="metadata=res://*/Models.DBModel.csdl|res://*/Models.DBModel.ssdl|res://*/Models.DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=MY-PC;initial catalog=DB;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" 
Batish answered 6/5, 2015 at 18:35 Comment(1)
I'm sorry, it had tags so it didn't appear because of formatting. I have removed tags and its thereBatish
T
17

You say "within my DAL, I have a webConfig". I guess the connection string is in the configuration file of a referenced class library, but not in the main configuration file you have in your entry project (a web api project, I guess looking at the tags).

If so, just copy the connection string in the entry project configuration file.

Tunicate answered 6/5, 2015 at 18:42 Comment(2)
I did add the connection string to webapi and I now get the following, {"Schema specified is not valid. Errors: \r\nThe mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type Client}Batish
Does this help? #14927891Tunicate
B
53

In the DBContext file, remove

public RaficaDB()
: base("name=DefaultConnection"){}

to

public RaficaDB()
: base("DefaultConnection"){}

EF 4.3, EF 5 and EF 6 do not like the connection string being called name=xxxxx

Answer found here -> No connection string named 'MyApplicationEntities' could be found in the application config file

Berndt answered 8/6, 2016 at 8:36 Comment(1)
If you supply name=xxx to the constructor, it will look for that specific connection string and throw an error if not set. If you don't specify name=xxx, the localdb/sqlexpress will attempt to create it. msdn.microsoft.com/en-us/library/jj592674(v=vs.113).aspxMaiden
T
17

You say "within my DAL, I have a webConfig". I guess the connection string is in the configuration file of a referenced class library, but not in the main configuration file you have in your entry project (a web api project, I guess looking at the tags).

If so, just copy the connection string in the entry project configuration file.

Tunicate answered 6/5, 2015 at 18:42 Comment(2)
I did add the connection string to webapi and I now get the following, {"Schema specified is not valid. Errors: \r\nThe mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type Client}Batish
Does this help? #14927891Tunicate
T
4

Insert following section in the configuration section of the .config file of the same project where your .edmx file is under.

You may also create different connection string for different environment in the .config file of the main project and pass any of the connection string as parameter of the constructor of the DBContext.

<connectionStrings>
  
<add name="DBEntities" connectionString="metadata=res://*/Models.DBModel.csdl|res://*/Models.DBModel.ssdl|res://*/Models.DBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MY-PC;initial catalog=DB;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

</connectionStrings>
Topple answered 6/5, 2015 at 18:55 Comment(1)
Please rename the web config to app.config in DAL project then.Topple
B
3

Setting the project as Startup project worked for me

Bloodstain answered 12/12, 2019 at 12:5 Comment(1)
Likewise; I was doing a code migeration from the Package Manager Console and it couldn't find my connection string in the app.config BECAUSE it was reading the app.config of a different project.Helgahelge
K
2

I found that this worked:

1) Check if you have several "App.config" files. 2) Check if has a wrong name than the connection string it has to use. 3) Save the project and run the program

It should work now.

Khudari answered 30/11, 2015 at 21:26 Comment(0)
T
1

Copy and paste the connectionstring to your WEBAPI Project web.config file will solve the issue.

Transarctic answered 29/4, 2016 at 12:40 Comment(0)
P
1

It's dumb, but I had this error that was fixed by a Rebuild All !!
Might as well have turned it off and on again....

Prater answered 27/6, 2018 at 6:38 Comment(0)
F
1

If none of the above fixes the issue, then probably you are doing the mistake I did, here was my case: I have multiple projects in my solution and the Startup project was different than the one having the entity framework, switching project from the package manager console seems like a buggy thing especially in entity framework commands, so here what I did:

  1. Set your webapi(Or the project has the entity framework) as a startup project.
  2. Rebuild the solution and try again.
  3. Run the entity framework command again.

If the above doesn't work then try to close the solution and try from Step 2, this should fix it.

Frissell answered 10/4, 2020 at 7:17 Comment(0)
A
0

The easiest solution:

Remove current edmx file and related connection string in app.config and add Edmx item with same name as previous again.

this worked for me.

Algeciras answered 15/1, 2016 at 17:26 Comment(0)
L
0

Double-check your /bin/ folder to make sure your built app's config (e.g. Foo.exe.config) has the connection string you're expecting.

If it's missing there, but present in your App.config, then in Solution Explorer, check the Properties of that App.config file. Make sure "Copy to Output Directory" is not "Do not copy". For example, try "Copy always". Then rebuild.

This might have accidentally been set to "Do not copy" if you copied the App.config file from a different project.

Labrum answered 18/7, 2024 at 16:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.