Entity Framework 4–Load metadata exception
What could be more simple?
I’ve created Console project (same for Test projects) to check my DAL created with EntityFramework 4. Everything looked Located right, everything compile, and yet, I keep get the following message:
“The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.”
So I open Google to find out what it is. I found some great posts, talk about a case like mine – when the edmx model is not the executed one. Some said copy the config file, some said edit the connection string and tell to EF where to find the Meatadata,
But somehow – still nothing worked for me.
After a short investigation, I discovered that when the model is called the first time, all the metadata uploaded to memory, according to the metadata section in the connection string:
connectionString="metadata=res://*/DataModel.csdl| res://*/DataModel.ssdl| res://*/DataModel.msl; provider=System.Data.SqlClient; provider connection string=" data source=.\sql2008; initial catalog=AdventureWorks; integrated security=True; multipleactiveresultsets=True; App=EntityFramework""
My solution actually contains three projects: one containing the model, second containing the Entities created by using the SelfTracking template, and the third – the console project I ran.
In fact, only because I called only classes from the second and third projects –the DLL that contains the metadata files as a resource not yet been loaded to memory, so the metadata didn’t found.
When ObjectContext constructor called, he actually scans all loaded files for metadata resource, and if he cant find it, he throws the exception above.
All you need do is force the loading of the metadata DLL, like calling Assembly.LoadFile () or something similar, or by simply declare a class from that DLL, and it will load automatically.