无法找到 EntityType 错误的 EF 映射和元数据信息
我在使用 Entity Framework 4.0 RC 时遇到异常。 我的实体框架模型封装在一个名为 Procurement.EFDataProvider 的私有程序集中,我的 POCO 类位于另一个程序集 Procurement.Core 内 Core(业务逻辑)和 EFDataProvider(数据访问)之间的关系与名为 DataProvider 的工厂相关,
因此当我尝试创建对象集时
objectSet = ObjectContext.CreateObjectSet<TEntity>();
出现错误:
找不到 EntityType“Procurement.Core.Entities.OrganizationChart”的映射和元数据信息。
I have encountered an exception when I use Entity Framework 4.0 RC.
My Entity Framework model is encapsulated in a private assembly who's name is Procurement.EFDataProvider and my POCO classes are inside of another assembly Procurement.Core
The relation between Core(Business Logic)and EFDataProvider(Data Access) is with a factory named DataProvider
so when I try to create an objectset
objectSet = ObjectContext.CreateObjectSet<TEntity>();
I get an error:
Mapping and metadata information could not be found for EntityType 'Procurement.Core.Entities.OrganizationChart'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
对于处理该错误的其他人,我认为值得一提的是我发现的一些导致此(极其无益)错误的场景:
也可能有其他原因。
华泰
For anyone else dealing with the error, I think it's worth mentioning some scenarios that I've found that cause this (extremely unhelpful) error:
There might be other causes as well.
HTH
这可能是因为 EF 找不到嵌入的映射信息。在您的连接字符串中,您可能会看到类似的内容:
* 是一个通配符,告诉对象上下文尝试从扫描所有加载的程序集中找到嵌入的映射信息。如果程序集未加载,EF 将找不到它。
您需要做的是向连接字符串提供有关映射信息嵌入位置的更多信息。将 * 更改为映射代码的特定程序集名称:
如果失败,请找到该程序集并使用以下命令将其直接加载到您的 ObjectContext 中:
This is probably because EF can't find the embedded mapping information. Inside your connection string you'll probably have something like his:
That * is a wildcard, telling the object context to try and find the embedded mapping information from, I think, scanning all the loaded assemblies. If the assembly isn't loaded, EF won't find it.
What you need to do is provide the connection string with more information about where your mapping information is embedded. Change the * to the specific assembly name of your mapping code:
If that fails, find the Assembly and directly load it into your ObjectContext using:
与上述内容没有直接关系,但如果您收到此错误消息并且您混合了 POCO 和常规模型:坏主意!
另请参阅 JRoppert 的评论 EF4 POCO(不使用 T4):找不到 EntityType 的映射和元数据信息(感谢 JRoppert!)
Not directly related to the above, but if you get this error message and you have mixed a POCO and a regular model: bad idea!
See also the comment from JRoppert at EF4 POCO (not using T4): Mapping and metadata information could not be found for EntityType (thanks JRoppert!)
我收到此错误是因为我在同一程序集中有多个 edmx 文件,而没有正确使用自定义命名空间。
中关于异常的说明:
以下是 System.Data.Objects.ObjectContext // Exceptions
I was getting this error because I had more than edmx file in the same assembly with out proper use of custom namespaces.
Here is what is said about the exception in System.Data.Objects.ObjectContext
// Exceptions:
当配置文件中未指定连接字符串时我也看到过它。
I have also seen it when the connection string is not specified in the config file.
另一个可能的问题是,如果您使用代码优先,并且您的实体类型在单独的程序集中定义到定义上下文的位置,并且您尚未添加任何自定义映射到
OnModelCreating(DbModelBuild modelBuilder)
方法,则 EF 似乎会忽略数据注释。解决方案:
将
modelBuilder.Entity();
添加到OnModelCreating(DbModelBuild modelBuilder)
方法。相关帖子。
Another possible issue is, if you are using code-first and your entity type is defined in a separate assembly to where the context is define and you haven't added any custom mappings to the
OnModelCreating(DbModelBuild modelBuilder)
method, then EF seems to ignore the data annotations.Solution:
Add
modelBuilder.Entity<YourEntityType>();
to theOnModelCreating(DbModelBuild modelBuilder)
method.Related post.
只需检查模型的属性拼写
Just check the property spelling with the model
可能还有另一个原因。我还拉了我一晚上的头发。
我发现解决方案中的参考文献有些混乱。在我的项目中,
edmx
属于一个名为DataAccess.Dll
的项目。但我没有从我的 exe 中引用它。从我的 exe 中,我引用了另一个名为Business.Dll
的项目,并且该项目引用了DataAccess.DLL
的旧位置。进行以下测试,看看您是否有这样的问题:
您需要检查您的引用并确保它们引用您的 dll 的更新位置。
There might be another reason. I also pulled my hair for a night.
I turned out that there is some disorder in the references in the solution. In my project, the
edmx
belongs to a project calledDataAccess.Dll
. But I have no reference to it from my exe. From my exe I have a reference to another project calledBusiness.Dll
, and this project has a reference for and old location for theDataAccess.DLL
.Make the following test to see if you have such problem:
You need to check your references and to ensure they refer to the update locations of your dlls.
就我而言,这本质上是同一个问题,映射并不完全符合预期。我遇到的情况是,我将“每个层次结构继承表”中的一个属性从子类之一移动到基类,但忘记更新模型。
我有自定义 POCO 并通过将 edmx 文件复制到一个空白的新项目并让它自动生成实体,然后将它们与我帮助我找到差异的内容进行比较。
In my case it was essentialy the same issue, the mapping not being exactly as expected. What happened to me was that I had move a property in a "Table Per Hierarchy inheritance" from one of the subclasses to the base class and forgot to update the model.
I have custom POCO and by copying the edmx file to a blank new project and let it autogenerate the entities, then compare them to what I had helped me in finding the difference.
一个菜鸟错误,但是当我使用只读用户名和密码访问数据库时,我遇到了错误。希望我的错误能帮助别人。
a noob mistake, But I had the error when my access to the DB was with a Read Only username and password. Hope that my mistakes help others.
我有类似的问题。我已经使用 POCO 类和我自己编写的 Context 对象为一个数据库创建了一个 EDMX 文件。当我为不同的数据库添加第二个 EDMX 时,我使用了 POCO T4 模板,然后 EDMX 都不起作用并引发了您提到的错误。为了解决这个问题,我放弃了自定义 POCO 和 Context,只使用 T4 模板,一切都恢复正常。
I had a similar problem. I already had one EDMX file for one database using POCO classes and a Context object I wrote myself. When I added a second EDMX for a different database I used the POCO T4 template and then neither EDMX worked and threw the error you mentioned. To resolve it I scrapped my custom POCO and Context and used only the T4 Template and all worked well again.
我在向表中添加了一些列时遇到了问题。
在表映射中,我重命名了列名称。
尽管我已经运行了“转换所有模板”并重建了应用程序,但我仍然得到了“类型 <> 的关联元数据类型”。包含以下未知属性或字段<>'错误。
Domain.Poco.tt 中该表的类是正确的,但我发现 Domain.Poco.MetaData.tt 中相应的 class.Metadata.cs 文件尚未更新,并且具有原始名称的新列 - 不是新的我在表映射中指定的。
解决方案?我刚刚删除了有问题的元数据类,然后重新运行“转换所有模板”,并且使用正确的列/函数名称正确地重新创建了它。
I had a problem where I had added some columns to a table.
In the Table Mappings, I had renamed the column names.
Although I had run 'Transform All Templates' and rebuilt the application, I still got the 'The associated metadata type for type <> contains the following unknown properties or fields <>' error.
The class for this table in Domain.Poco.tt was correct, but I found the corresponding class.Metadata.cs file in Domain.Poco.MetaData.tt had not updated, and had the new columns with the original names - not the new ones I'd specified in Table Mapping.
Solution? I just deleted the offending metadata class, and re-ran 'Transform All Templates' and it was recreated correctly, with the correct column/function names.
我的问题是我编辑了 T4 模板以排除名为“msrepl_tran_version”的复制字段。这导致数据库与生成的类不匹配,从而导致此错误消息。只需确保您的数据库和类匹配即可。
My problem was I had edited the T4 template to exclude the replication field named "msrepl_tran_version". This caused the database to not match the generated classes which can causes this error message. Just make sure your database and classes match.