无法找到 EntityType 错误的 EF 映射和元数​​据信息

发布于 2024-08-23 01:42:52 字数 421 浏览 4 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

噩梦成真你也成魔 2024-08-30 01:42:52

对于处理该错误的其他人,我认为值得一提的是我发现的一些导致此(极其无益)错误的场景:

  • 拼写错误的属性(区分大小写!)
  • POCO 类中缺少属性
  • POCO 和实体之间的类型不匹配 POCO 中的类型(例如,int 而不是 long)
  • 枚举(据我所知,EF 目前不支持枚举)

也可能有其他原因。

华泰

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:

  • Misspelled properties (case-sensitive!)
  • Properties missing in the POCO class
  • Type mismatches between the POCO and entity-type (e.g., int instead of long)
  • Enums in the POCO (EF doesn't support enums right now as I understand)

There might be other causes as well.

HTH

一念一轮回 2024-08-30 01:42:52

这可能是因为 EF 找不到嵌入的映射信息。在您的连接字符串中,您可能会看到类似的内容:

metadata=res://*/Models.MyModels.csdl|...etc

* 是一个通配符,告诉对象上下文尝试从扫描所有加载的程序集中找到嵌入的映射信息。如果程序集未加载,EF 将找不到它。

您需要做的是向连接字符串提供有关映射信息嵌入位置的更多信息。将 * 更改为映射代码的特定程序集名称:

metadata=res://Procurement.EFDataProvider/Models.MyModels.csdl

如果失败,请找到该程序集并使用以下命令将其直接加载到您的 ObjectContext 中:

ObjectContext.Metadataworkspace.LoadFromAssembly();

This is probably because EF can't find the embedded mapping information. Inside your connection string you'll probably have something like his:

metadata=res://*/Models.MyModels.csdl|...etc

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:

metadata=res://Procurement.EFDataProvider/Models.MyModels.csdl

If that fails, find the Assembly and directly load it into your ObjectContext using:

ObjectContext.Metadataworkspace.LoadFromAssembly();
温暖的光 2024-08-30 01:42:52

与上述内容没有直接关系,但如果您收到此错误消息并且您混合了 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!)

贱贱哒 2024-08-30 01:42:52

我收到此错误是因为我在同一程序集中有多个 edmx 文件,而没有正确使用自定义命名空间。

中关于异常的说明:

以下是 System.Data.Objects.ObjectContext // Exceptions

    //   System.InvalidOperationException:
    //     When the System.Data.Metadata.Edm.EntitySet from entitySetName
    //     does not match the System.Data.Metadata.Edm.EntitySet of the object’s
    //     System.Data.EntityKey.
    // -or-
    //     When the System.Data.Objects.ObjectContext.DefaultContainerName
    //     property is not set on the System.Data.Objects.ObjectContext and 
    //     the name is not qualified as part of the entitySetName parameter.
    // -or-
    //     When the specified type belongs to more than one entity set.

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:

    //   System.InvalidOperationException:
    //     When the System.Data.Metadata.Edm.EntitySet from entitySetName
    //     does not match the System.Data.Metadata.Edm.EntitySet of the object’s
    //     System.Data.EntityKey.
    // -or-
    //     When the System.Data.Objects.ObjectContext.DefaultContainerName
    //     property is not set on the System.Data.Objects.ObjectContext and 
    //     the name is not qualified as part of the entitySetName parameter.
    // -or-
    //     When the specified type belongs to more than one entity set.
幸福还没到 2024-08-30 01:42:52

当配置文件中未指定连接字符串时我也看到过它。

I have also seen it when the connection string is not specified in the config file.

铁憨憨 2024-08-30 01:42:52

另一个可能的问题是,如果您使用代码优先,并且您的实体类型在单独的程序集中定义到定义上下文的位置,并且您尚未添加任何自定义映射到 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 the OnModelCreating(DbModelBuild modelBuilder) method.

Related post.

落日海湾 2024-08-30 01:42:52

只需检查模型的属性拼写

Just check the property spelling with the model

避讳 2024-08-30 01:42:52

可能还有另一个原因。我还拉了我一晚上的头发。

我发现解决方案中的参考文献有些混乱。在我的项目中,edmx 属于一个名为 DataAccess.Dll 的项目。但我没有从我的 exe 中引用它。从我的 exe 中,我引用了另一个名为 Business.Dll 的项目,并且该项目引用了 DataAccess.DLL 的旧位置。

进行以下测试,看看您是否有这样的问题:

  1. 打开您的 exe 项目的 bin 目录并使其保持可见。
  2. 构建解决方案。
  3. 将构建的第一个项目是数据访问项目,您可以在 bin 文件夹中看到当前时间作为其修改日期。
  4. 当构建其他项目时,您将看到 DataAccess 项目的修改日期已更改为旧日期。

您需要检查您的引用并确保它们引用您的 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 called DataAccess.Dll. But I have no reference to it from my exe. From my exe I have a reference to another project called Business.Dll, and this project has a reference for and old location for the DataAccess.DLL.

Make the following test to see if you have such problem:

  1. Open the bin director of your exe project and keep it visible.
  2. Build the solution.
  3. The first project that will be built is the data access project, and you can see in the bin folder the current time as its modified date.
  4. While other projects are built, you will see that the modified date of the DataAccess project was changed to an old one.

You need to check your references and to ensure they refer to the update locations of your dlls.

贪了杯 2024-08-30 01:42:52

就我而言,这本质上是同一个问题,映射并不完全符合预期。我遇到的情况是,我将“每个层次结构继承表”中的一个属性从子类之一移动到基类,但忘记更新模型。

我有自定义 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.

陌生 2024-08-30 01:42:52

一个菜鸟错误,但是当我使用只读用户名和密码访问数据库时,我遇到了错误。希望我的错误能帮助别人。

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.

花开半夏魅人心 2024-08-30 01:42:52

我有类似的问题。我已经使用 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.

踏雪无痕 2024-08-30 01:42:52

我在向表中添加了一些列时遇到了问题。

在表映射中,我重命名了列名称。

尽管我已经运行了“转换所有模板”并重建了应用程序,但我仍然得到了“类型 <> 的关联元数据类型”。包含以下未知属性或字段<>'错误。

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.

韬韬不绝 2024-08-30 01:42:52

我的问题是我编辑了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文