views:

1182

answers:

3

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?

+2  A: 

Not sure about this one... it does seem a little wierd, so this is a long shot.

But sometime the occasional ObjectContext.MetadataWorkspace.LoadFromAssembly() call helps.

And even if it doesn't there is a second overload that provides trace style output.

i.e.

Assembly assembly = typeof(AnnouncementText).Assembly;
context.MetadataWorkspace.LoadFromAssembly(
    assembly, 
    (message) => Console.WriteLine(message)
);
foreach(var at in context.AnnouncementTexts)
   ...

And see what messages you get (if any).

Hope this helps

Alex

Alex James
Well, that doesn't seem to do (or print) anything in this case. I also tried removing the POCO stuff and using the default entity generation with your example code, and I'm afraid I didn't see any output in that case either.Thanks for the attempt! Shots in the dark are more than welcome, heh.
ladenedge
+1  A: 

Well, I've been successfully using the RC-compatible POCO Template for about a week now without any issues. I guess the new configuration has saved me from whatever it is I was doing wrong.

Edit: I recently ran into this same error while moving my EDMX file to a new location in the solution. After spending a good amount of time on the problem, I think I've discovered the solution to my original problem as well. Hopefully this will help you out, too.

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 (1), another that appears in the SSDL which looks something like this (2):

<Schema Namespace="..." ..

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

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

Similarly, namespace (3) is only relevant in the way C# namespaces usually are.

Here's the problematic part. Category (4) 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 (CSDL, SSDL, and MSL). If you're not sure, check out your compiled assembly's resources in Reflector.

ladenedge
Awesome, exactly what I was looking for. I had moved my edmx file to within a new folder, and indeed, the missing namespace information in the metadata connectionstring for EF was the culprit. Thanks!
kdawg
A: 

Well. I am getting this error in the following senario.

1.I have a situation where i have 2 tables.

A and B A table has 8 fields --> Code being the primary key

B has many fields but has a column called Acode which is in relation with code of A (FK).

Now when i have only A table in the EDMX file. All the methods of the repository operations happen correctly. The moment i add B table into the EDMX (Update from database) and i try to run the get, update, insert from db for region only. They all fail citing incompatibility and the error mentioned above System.InvalidOperationException: Mapping and metadata information could not be found for EntityType..

I am using Stored procedures for all these operations. B also also has a Fk relation with another table called C.

Rohit