"Metadata information not found" while using EF4's POCO Template?
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 MyName开发者_开发百科space.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.
If you copy an EDMX file to another project, the Build Action (in the Properties pane) will revert to the default value of "None" in the target project, which can cause the "EntityContainer name . . . could not be found" error, which is because no resources are being generated. (If this is the case, when you open the DLL containing the model in Reflector there will be no resources.) Setting the Build Action to "EntityDeploy" and rebuilding the solution will correct this.
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
On the Solution Explorer, right click on the Entity Data Model file (.edmx) and click 'Open with'. A dialog box will open up to choose a program for you to open the file. Select or Double click on 'XML (text) Editor'.
Now you re looking at the XML version of the EDM file where you can easily edit the 'EntityContainer' name.
If you have changed the connectionstring on the webconfig file, make sure the 'DefaultContainerName' is the name on section 'EntityContainer' in that XML file.
Usually when you update the defaultContainerName in *.Designer.cs file, it doesn't update the XML file. So you have do it manually.
I am extending NopCommerce by creating a separate project and an edmx inside it. POCO templates are used to generate my custom ObjectContext
descendant class.
I verified all the properties, all the namespaces and still was getting the abovementioned exception.
The point was to remove MyModel.Designer.cs file that generated another ObjectContext
descendant class (with another name and inside another namespace).
we have the same problem using no edmx model, we us EF powertools beta 2, with reverse engineering code first feature to generate our entities, the context and the views.
and we have the strange behavior, that in team on several machines the hash for the generated compiled views is different, and then on TFS team build same as on the other machines we get an exception like following . Question is based on what the power tools is generating the hash for the compiled views ? and what we can change that it is consistent, we are using the power tools beta 2 with EF 4.3.1 , system.data .v4.0.30319 ,against an Oracle db using the devart oracle drivers.
context generate views
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version:4.0.30319.551
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
//
//------------------------------------------------------------------------------
[assembly: System.Data.Mapping.EntityViewGenerationAttribute(typeof(Edm_EntityMappingGeneratedViews.ViewsForBaseEntitySetsC6473E0A11A196A36CC5839589DE2DE553F202E361016A89F2142797168B2534))]
namespace Edm_EntityMappingGeneratedViews
{
/// <Summary>
/// The type contains views for EntitySets and AssociationSets that were generated at design time.
/// </Summary>
public sealed class ViewsForBaseEntitySetsC6473E0A11A196A36CC5839589DE2DE553F202E361016A89F2142797168B2534 : System.Data.Mapping.EntityViewContainer
{
/// <Summary>
/// The constructor stores the views for the extents and also the hash values generated based on the metadata and mapping closure and views.
/// </Summary>
public ViewsForBaseEntitySetsC6473E0A11A196A36CC5839589DE2DE553F202E361016A89F2142797168B2534()
{
this.EdmEntityContainerName = "Context";
this.StoreEntityContainerName = "CodeFirstDatabase";
this.HashOverMappingClosure = "2c6bbce22db7c9c65f8a70c5a1bae1225522a2124aad50e74bdc186ce8c70508";
this.HashOverAllExtentViews = "26cad9d7334571a0116f89413345d00ec8e031706228df6e653c7b6396c94591";
this.ViewCount = 56;
}
exception :
System.Data.EntityCommandCompilationException: System.Data.EntityCommandCompilationException: An error occurred while preparing the command definition. See the inner exception for details. ---> System.Data.MappingException: The mapping and metadata information for EntityContainer 'Context' no longer matches the information used to create the pre-generated views..
I had a similar issue when integrating various projects.
The issue was that I had more than one edmx
since I had many projects.
The connection string was the same, but the namespace was different.
To solve this, choose one of the connections to use and change the others.
Be aware that you will need to make the change in the designer.cs and also in the xml file.
Had a similar problem after changing the view sql. Has to right mouse on the tt file to update it and that fixed it.
精彩评论