“未找到元数据信息".在使用EF4的POCO模板时? [英] "Metadata information not found" while using EF4's POCO Template?

查看:136
本文介绍了“未找到元数据信息".在使用EF4的POCO模板时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了EF4的POCO模板.我的模型中只有一个实体 AnnouncementText ,并且T4文件似乎已正确生成.当我访问自动生成的属性MyObjectContext.AnnouncementTexts时,尝试访问此新实体会引发以下错误:

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:找不到EntityType'MyNamespace.AnnouncementText'的映射和元数​​据信息.

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

AnnouncementText POCO上的属性似乎与数据库中的列匹配,并且我没有更改任何自动生成的代码.

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.

堆栈跟踪为:

   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

如果我从解决方案中删除了.tt文件,并在模型上启用了代码生成,那么我将能够毫无问题地访问该属性.

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?

推荐答案

我最近在将EDMX文件移动到解决方案中的新位置时再次遇到了相同的错误.显然,处理EDMX文件时有几个不同的名称空间.创建初始EDMX文件(N1)时,您可以通过向导输入名称空间,该名称空间出现在SSDL中,看起来像这样(N2):

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="..." ..

然后是生成的代码的名称空间,可以在设计器(N3)中指定(可选),最后是隐藏到资源中的隐藏的名称空间,这些资源将被编译到最终程序集中(N4).

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).

据我所知,名称空间 N2 仅在SSDL内部真正相关.我相信此命名空间以 N1 开头-您最初在向导中输入的那个命名空间.

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.

类似地,命名空间 N3 仅与C#命名空间通常相关.

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

这是有问题的部分.类别 N4 名称空间是EDMX所在的目录的功能(相对于您的项目目录) .您可能会想,那又如何呢?事实证明,您的App.config文件中也引用了那些名称空间!具体来说,寻找这样的一部分:

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|...

读取"Database.Master.csdl"的那部分是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:

在映射和元数​​据信息中找不到指定的默认EntityContainer名称'[name]'.

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

简单的解决方案是更改App.config,以为EF映射的每个部分(CSDL,SSDL和MSL)指定正确的资源名称.如果不确定这些名称到底是什么,请在 ILSpy 中查看编译的程序集的资源. dotPeek .

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.

这篇关于“未找到元数据信息".在使用EF4的POCO模板时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆