& Ldquo; Metadata information not found & rdquo; While using the POCO EF4?

advertisements

I just installed the POCO Template for EF4. I have a single entity in my model, AnnouncementText, and the T4 files seem to be properly generated. Attempting to access this new entity is throwing the following error when I access the auto-generated property MyObjectContext.AnnouncementTexts:

InvalidOperationException: Mapping and metadata information could not be found for EntityType 'MyNamespace.AnnouncementText'.

The properties on the AnnouncementText POCO seem to match up with the columns in the database, and I haven't changed any of the auto-generated code.

The stack trace is:

   at System.Data.Objects.ObjectContext.GetTypeUsage(Type entityCLRType)
   at System.Data.Objects.ObjectContext.GetEntitySetForNameAndType(String entitySetName, Type entityCLRType, String exceptionParameterName)
   at System.Data.Objects.ObjectContext.CreateObjectSet[TEntity](String entitySetName)
   at MyNamespace.MyObjectContext.get_AnnouncementTexts() in C:\<snip>\MyObjectContext.Context.cs:line 65
   at MyNamespace.Class1.Main() in C:\<snip>\Class1.cs:line 14

If I delete the .tt files from the solution and enable code generation on the model, I am able to access the property without issue.

Here's my code, in case that might help:

using (var context = new MyObjectContext())
   foreach (var at in context.AnnouncementTexts)
      Console.WriteLine(at.Title);

Any ideas on what might be wrong?


I recently ran into this same error again while moving my EDMX file to a new location in the solution. Apparently, there are a couple of different namespaces when dealing with EDMX files. There is the namespace you enter via the wizard when creating the initial EDMX file (N1), another that appears in the SSDL which looks something like this (N2):

<Schema Namespace="..." ..

Then there's the namespace of the generated code which may (optionally) be specified in the designer (N3), and finally there are the hidden namespaces of the resources that are compiled in to your final assembly (N4).

From what I can tell, namespace N2 is only really relevant inside the SSDL. I believe this namespace starts off as N1 - the one you initially enter in the wizard.

Similarly, namespace N3 is only relevant in the way C# namespaces usually are.

Here's the problematic part. Category N4 namespaces are a function of the directory in which your EDMX resides (relative to your project directory). You might think, So what? It turns out those namespaces are also referenced in your App.config file! Specifically, look for a part like this:

connectionString="metadata=res://*/Database.Master.csdl|...

That portion reading "Database.Master.csdl" is the name of your CSDL resource. If those resource names get out of sync, you'll receive an error like the one above, or perhaps:

The specified default EntityContainer name '[name]' could not be found in the mapping and metadata information.

The simple solution is to alter your App.config to specify the correct resource name for each part of your EF mapping (CSDL, SSDL, and MSL). If you're not sure exactly what those names are, check out your compiled assembly's resources in ILSpy or dotPeek.