1
votes

I'm getting the following error:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

I've just created my first ever EF project. It's an MVC app and I added the entity model in the MVC project. I also added a DataAccess class and a class for running tests using NUnit. Eventually, I'll add a service class which will reference the DataAccess class. So, the code currently looks like this (I'm just trying to get a test working to prove EF is doing its thing):

  • Text Fixture calls DataAccess class
  • DataAccess class calls Entity Framework
  • Entity Framework accesses a local database

For the time being, I'm just trying to return all rows/one column from one table. Remember, all these files are in a single project. I've read quite a lot that this problem stems from having multiple projects, but that doesn't seem to apply in my case. I've checked in the "main" web.config file. The connection string looks okay. I copied that same config section (i.e., connectionStrings) into the Debug-specific config file, too, but that didn't make a difference. Any ideas why I'm seeing this error?

Thanks, Jay

Snapshot of the solution tree

Connection strings:

This is the connection string from the dialog box when creating the Entity Access file (data source is a period in both strings [i.e., local host]):

metadata=res:///EntityDataModel.csdl|res:///EntityDataModel.ssdl|res://*/EntityDataModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.;Initial Catalog=URIntake;Integrated Security=True"

This is the connection string from the web.config file. They seem the same, for all practical purposes:

metadata=res:///EntityDataModel.csdl|res:///EntityDataModel.ssdl|res://*/EntityDataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=URIntake;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"

1

1 Answers

1
votes

Microsoft Zlatko Michailov Says,

app.config is not in the binary directory where the exe is. Please do the following:

  1. Visually verify that the app.config with the expected content is in the directory where the exe is compiled. (Existence in the project root directory is not enough.)

  2. Use System.Configuration.ConfigurationManager from within your app to examine the content of the app.config your exe is using.

I’m also looking at the content of the connection string, and I can say that it may not work in a multi project environment (unless you’ve duplicated the EDM in each project).

The reason for that is “.” resolves to the directory where the exe is loaded from. If you want to reuse the same EDM, you at least have to make a few steps back in the path and then navigate to the project where the EDM is, e.g. “......\Proj1\AdventureWorksModel”.

Additionally you may consider using the |DataDirectory| macro - when you load an AppDomain you can set |DataDirectory| to point to the exact directory where the EDM is, and then use that in the connection string, e.g. “|DataDirectory|\AdventureWorksModel”.

If you are working on an ASP.NET project, you can use “~” which refers to the project root. In that latter case, you can’t reference a model outside your project’s hierarchy though.

For more information Check Here

UPDATE 1 :

Here you can try below mentioned steps

  1. Clear connection string content on the web.config file like below

    enter image description here

  2. Then Remove your *.edmx file from your project

  3. Recreate it again like below (sample one).Don't forget to tick the "save entity conncetion settings in web.config as :"

enter image description here

Final Step : After that go to the web.config file and check whether your connection string is exactly the same as on which showed on "Entity Connection String :" as above step(I showed it on red mark above image).

I hope this will help to you.