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

12

171

I have a one entity framework object and when I add it to my project, the connectionstring is added to app.config in the connectionstring section, but when I want to create new entitycontext and use this connectionstring, this error appears

Karnak answered 16/8, 2010 at 6:58 Comment(3)
Related: MetadataException: Unable to load the specified metadata resourceIlliquid
Thanks for the link, Craig. I vote to keep this thread, though, because the title was what helped me find the MetadataException error.Numerable
This happened to me when the config file did not update with a connection string for some strange reason.Renunciation
R
218

I suspect that your issue is coming from the fact that you have more than one project in your solution and the one that contains your entity framework stuff including edmx files is NOT the solutions's startup project. In this case even if the connection string exists in the EF app.config project, still CLR cannot find it at runtime. For example, if you have a web site and a EF project in your solution, you need to copy the connection string from the EF project's app.config to your website's web.config. Basically, any connection string data should exist in the config file of the project that the .Net threads initiated from by CLR (i.e. your startup project). If this is not your case, then just open your edmx file, right click on its surface, select properties and copy the connection string and paste it into your app.config Connection String section. This way you can make sure that you are having the correct one in your config.

EDIT:
As you can see here on Documenation on ObjectContext Constructor, the first parameter is the connectionstring name which is code generated at the time you create your EDM. If, somehow, the name of your connectionstring name happens to be changed, all you need to do is right clicking on your model and selecting "Update Model From Database..." then follow the wizard to update your confing and designer to reflect this change.

Rawden answered 17/8, 2010 at 4:22 Comment(5)
hi Morteza and thank for your answer, but i previously copy the connectionstring sectin in web.config, but the error is not solved, but when in entitymodel.designer replace (public EntityContext(): base("name=EntityContext", EntityContext")) whit the connectionstring this worked, any idaeKarnak
@Morteza, do you have a solution for projects in which the executing assembly doesn't have an app.config? In my case the calling executable is a VB6 app that calls my assembly (where the entity objects are) through COM Interop.Strophanthus
Such a useful piece of information. Somewhere this should get connected to the exception (System.ArguementException) because I didn't find this answer searching for that and instead decided to break my serialization trying something stupid :) this only comes up when you search the exceptions message.Morris
If you are using WCF, remember to include the connection string in your service projectTadeas
fyi, also if you are unit testing make sure the connection string is in the unit test project app.config file.Wilden
B
32

You need to copy the connection string in the app.config to your web.config, or copy the entire file to the project which displays the output. It is one of the conditions to consume the framework.

Balbur answered 29/10, 2011 at 13:57 Comment(3)
just add the same connection string (as in Data access project) in your web.config (found in the front end project).Titoism
@Balbur I use EF and define all entity connection strings in code. I dont have anything in a config relating to it.Agentival
After I copied DAL files into a new project, I received the above error as well. Looking closer at the config files, i noticed that the connection string was replaced by EF. I was able to manually copy/paste the correct connection string into the new project and it worked.Phallic
S
9

I ran into this problem when I tried to put my custom database logic in a .dll to be used by multiple projects in my solution.

While the .dll had the correct app.config file, it didn't work. Entity frameworks wanted the connection information in the app.config of the .exe. Copying the information to there worked just fine.

Morteza's solution of pasting the connection string directly into the .edmx didn't work for me, as it wouldn't let me paste the value in there -- although that's precisely what I wanted to be able to do.

Stockjobber answered 27/9, 2010 at 20:48 Comment(1)
Even though there was only an app.config (and that was the only config file in that directory) in the directory of my exe it wouldn't read it. I had to rename the file myExe.exe.configBestiary
G
6

Hi I had this problem and it was making me nuts. Anyway finally I figured out what the problem was. First thing you have to do is make sure that the connectionstrings in app.config and web.config are the same. Then you must double click on the .edmx file so you can see the tables. Once u are click anywhere near the tables but not on the tables and go to properties. From the dropdown list select the ConceptualEntityModel and search for the Entity Container Name and remember it well.

Next go to the designer of the edmx file and open the constructors. (the designer is the subfolder of the edmx file) the constructors should have two parameters in the BASE parameter

public DBEntities() : base("name=DBEntities", "DBEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

this is one of them. the first parameter should have the name of the project file in which the .edmx file is in. The second parameter must have the name of the entity container name from the properties I mentioned earlier about. do not forget to arrange all the constructors with the : base("", "")

Atleast that was my problem and my problem was solved like that. I hope u manage to solve yours like this.

Greerson answered 29/11, 2012 at 18:0 Comment(0)
B
6

I had a variation on this that no-one seemed to cover.

I had a main project with a couple of models, and a Test Project containing unit tests. The Test Project was working, but then stopped with the error mentioned in the OP. I hadn't done any renaming or moving of the EDMX file.

A lot of the advice mentioned comparing .config files, but my project had none at all.

In the end, I copied the app.config file from the main project into my test project and then it worked. Whether this is the correct step, or will present maintainability issues when additional models are added, I do not know, but at least my unit tests are running correctly again now.

Bonhomie answered 11/12, 2012 at 16:30 Comment(1)
Since I posted this, I realised it would probably be best to create a link to the app.config file of another project. But still, the other approach will work for fixing the original problem when there is no config file at all.Bonhomie
B
4

Although Morteza Manavi' answer does solve this problem, another solution is to build the connection string dynamically and pass it into the constructor for your ObjectContext:

public static string CreateConnectionString()
{
    var assemblyPath = Assembly.GetExecutingAssembly().Location;
    string assemblyLocation = Path.GetDirectoryName(assemblyPath);
    string dbPath = Path.Combine(assemblyLocation, "YourDatabase.sdf");
    var sqlBuilder = new SqlConnectionStringBuilder { DataSource = dbPath };

    var entityBuilder = new EntityConnectionStringBuilder
    {
        ProviderConnectionString = sqlBuilder.ConnectionString,
        Provider = "System.Data.SqlServerCe.3.5",
        Metadata = @"res://*/YourModel.csdl|
                 res://*/YourModel.ssdl|
                 res://*/YourModel.msl"
    };

    return entityBuilder.ToString();
}

// Snip...

var entityContext = new YourObjectContext(CreateConnectionString());

This eliminates the need to copy the connection string information to the app.config of your startup project which, at least in my case, was not desirable.

Bookshelf answered 11/11, 2012 at 23:1 Comment(0)
E
4

I forgot to add providerName="System.Data.EntityClient" as attribute in the connection string. This resulted in this error so

<add name="connectionName" connectionString="metadata=res://*/..." providerName="System.Data.EntityClient" />

instead of

<add name="connectionName" connectionString="metadata=res://*/..." />
Essentialism answered 17/6, 2014 at 3:51 Comment(0)
O
2

I just found that if an app is created in IIS from VS2010 two levels from the website root this error would occur. Not sure why it happens, would need to investigate more. For example, if your app is in this path: /admin/advertiser the error would appear if you don't have /admin virtual directory in your IIS site.

All I did is created an empty admin directory in my .../intepub/wwwroot error disappeared.

You will find that you won't be able to start debugging until you do the step above.

We had this problem in our team in past, it took some time to remember but this was exactly how we fixed it before also.

Outpour answered 18/7, 2012 at 17:42 Comment(0)
L
1

I had a class library that didn't want to work with EF either. After I copied the app.config (or just the connectionstring section) from my class library to the exe project the connection worked fine! Probably the config file is expected to be in the same folder as the exe project and therefore was not found. So always be extra careful when a config file is used in a class library project!

Ligula answered 7/6, 2012 at 8:12 Comment(0)
T
1

I am using n'tier architecture and got the same problem but this one help me.hope this will help u. First you have same connection string on you libraries where you can access DB like in app.config and web.config after that you simply add a overloaded constructor in .edmx(Model.context.cs) file that now you have two constructor one is default and the other one u just added (overloaded).

        public YourEntityName(string connString)
            : base(connString)
        {
        }
Tammy answered 22/3, 2016 at 6:43 Comment(0)
C
0

Well... this problem could be also for a very simple (dumb) reason... I copied a file from another project and forgot to change the ConnectionString on the EntityDataSource... as I was at the beginning of the project and happened in the login page, I thought it was something on the config, but was just the wrong connection string name (and DefaultContainerName).

Crossed answered 11/3, 2020 at 22:14 Comment(0)
Z
0

So I had a similar issue with JetBrains Rider. What I was doing was using the green ▶ button next to the main function. This was causing the above message to show. If I instead right clicked on the project itself and clicked run, then it worked.

Not sure why this worked by I'm assuming it's because Rider does not read the app.config if running the individual function?

Zetes answered 23/2, 2022 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.