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

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
user421413
  • 1,731
  • 2
  • 11
  • 3
  • 1
    Related: [MetadataException: Unable to load the specified metadata resource](http://stackoverflow.com/questions/689355/metadataexception-unable-to-load-the-specified-metadata-resource) – Craig Stuntz Aug 16 '10 at 13:45
  • 4
    Thanks for the link, Craig. I vote to keep this thread, though, because the title was what helped me find the MetadataException error. –  May 23 '11 at 20:21
  • This happened to me when the config file did not update with a connection string for some strange reason. – P.Brian.Mackey Aug 23 '12 at 16:45

12 Answers12

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.

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
Morteza Manavi
  • 33,026
  • 6
  • 100
  • 83
  • 1
    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 idae – user421413 Aug 17 '10 at 05:14
  • 1
    @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. – Perishable Dave Dec 01 '11 at 19:13
  • 4
    so simple,yet so frustrating. Up VOTE. Long live stackoverflow! – granadaCoder Jun 09 '12 at 13:46
  • 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. – evanmcdonnal Jun 28 '12 at 00:02
  • 3
    If you are using WCF, remember to include the connection string in your service project – Nathan Oct 03 '12 at 02:54
  • fyi, also if you are unit testing make sure the connection string is in the unit test project app.config file. – Ronald McDonald Oct 16 '12 at 20:27
  • 1
    Thanks Morteza, saved me much time! – Chris May 23 '13 at 20:05
  • @Morteza you are a god! Thank you. – Jessica Sep 03 '18 at 11:31
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.

Musikero31
  • 3,115
  • 6
  • 39
  • 60
  • 1
    just add the same connection string (as in Data access project) in your web.config (found in the front end project). – Danish Jan 02 '12 at 08:00
  • @Musikero31 I use EF and define all entity connection strings in code. I dont have anything in a config relating to it. – Keith Beard Jan 19 '12 at 04:05
  • 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. – Ravi Ram May 22 '12 at 15:55
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.

Walt Stoneburner
  • 2,562
  • 4
  • 24
  • 37
  • 3
    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.config – Mario Jun 06 '11 at 20:31
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.

Jomoos
  • 12,823
  • 10
  • 55
  • 92
MaltaCoders
  • 61
  • 1
  • 1
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.

S. Baggy
  • 950
  • 10
  • 21
  • 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. – S. Baggy Aug 12 '13 at 12:57
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.

Community
  • 1
  • 1
Nick Spreitzer
  • 10,242
  • 4
  • 35
  • 58
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://*/..." />
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Gertjan
  • 519
  • 4
  • 5
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.

Jovica Zaric
  • 553
  • 5
  • 9
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!

Luis Gouveia
  • 8,334
  • 9
  • 46
  • 68
Vincent
  • 11
  • 1
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)
        {
        }
Muhammad Asad
  • 1,772
  • 16
  • 23
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).

Gabriel G
  • 211
  • 5
  • 7
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?

apokryfos
  • 38,771
  • 9
  • 70
  • 114