错误 3004:映射从行开始的片段时出现问题

发布于 2024-11-08 12:09:58 字数 1468 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(13

猫腻 2024-11-15 12:09:59

就我而言,另一位开发人员已从数据库的表中删除了该字段。意识到这一点后,从实体模型中删除该表并将其添加回来解决了问题。

In my case, another developer had deleted the field from the table in the database. Having realised this, removing the table from the entity model and adding it back solved the problem.

千仐 2024-11-15 12:09:59

我现在遇到了这个问题...需要使用 Visual Studio 中的设计器向现有表添加一个标量属性(例如 custom_property)。

我可以从数据库更新模型,但这不是一个选择。它在模型中造成了很多错误。这是一个巨大的数据库模型,我需要添加此属性的表可能有 30 多个关系。我只是想映射这个新的 custom_property 列,该列已添加到名为 custom_table 的表中。

直接在设计器中添加标量属性后,抛出以下异常:

  • InnerException {"\r\nModels.MyDB.msl(352,10):错误 3004:从第 352 行开始的映射片段出现问题:无映射
    为 Set 中的属性 custom_table.custom_property 指定
    custom_table。\r\n在以下情况下,具有密钥 (PK) 的实体将不会往返:\r\n
    实体类型为 [MyDB.custom_table]\r\n"} System.Exception
    {System.Data.Entity.Core.MappingException}

在 VS 的错误列表中显示:错误 11009:属性“ ”未映射

我采取的修复步骤this:

  1. 在 Visual Studio Code 中编辑 .edmx 文件;
  2. 寻找custom_table
  3. 使用映射补充了 .edmx XML 代码。

这些是我必须添加映射的地方:

<EntityType Name="custom_table">
    <Key>
        <PropertyRef Name="some_id" />
    </Key>
    <Property Name="some_id" Type="int" Nullable="false" />
    <Property Name="other_id" Type="int" />
    ...
    <Property Name="custom_property" Type="int" Nullable="true" /> <= THIS WAS ADDED BY ME
</EntityType>

<EntitySetMapping Name="custom_table">
  <EntityTypeMapping TypeName="MyDB.custom_table">
    <MappingFragment StoreEntitySet="custom_table">
      <ScalarProperty Name="some_id" ColumnName="some_id" />
      <ScalarProperty Name="other_id" ColumnName="other_id" />
      ...
      <ScalarProperty Name="custom_property" ColumnName="custom_property" /> <= THIS WAS ADDED BY ME
    </MappingFragment>
  </EntityTypeMapping>
</EntitySetMapping>

I hit this problem right now... Needed to add a scalar property, let's say custom_property to an existing table using the Designer in Visual Studio.

I could've updated the Model from the Database but that was not an option. It was causing a lot of errors in the Model. It's a huge database model and the table I needed to add this property has maybe more than 30 relationships. I just wanted to map this new custom_property column that got added to the table named custom_table.

After adding the scalar property directly in the designer, the following exception was being thrown:

  • InnerException {"\r\nModels.MyDB.msl(352,10) : error 3004: Problem in mapping fragments starting at line 352: No mapping
    specified for properties custom_table.custom_property in Set
    custom_table.\r\nAn Entity with Key (PK) will not round-trip when:\r\n
    Entity is type [MyDB.custom_table]\r\n"} System.Exception
    {System.Data.Entity.Core.MappingException}

In the Error List in VS this was being shown: Error 11009: Property ' ' is not mapped

Steps I took to fix this:

  1. Edited the .edmx file in Visual Studio Code;
  2. Looked for custom_table;
  3. Complemented the .edmx XML code with mappings.

These were the places where I had to add mappings:

<EntityType Name="custom_table">
    <Key>
        <PropertyRef Name="some_id" />
    </Key>
    <Property Name="some_id" Type="int" Nullable="false" />
    <Property Name="other_id" Type="int" />
    ...
    <Property Name="custom_property" Type="int" Nullable="true" /> <= THIS WAS ADDED BY ME
</EntityType>

and

<EntitySetMapping Name="custom_table">
  <EntityTypeMapping TypeName="MyDB.custom_table">
    <MappingFragment StoreEntitySet="custom_table">
      <ScalarProperty Name="some_id" ColumnName="some_id" />
      <ScalarProperty Name="other_id" ColumnName="other_id" />
      ...
      <ScalarProperty Name="custom_property" ColumnName="custom_property" /> <= THIS WAS ADDED BY ME
    </MappingFragment>
  </EntityTypeMapping>
</EntitySetMapping>
酒几许 2024-11-15 12:09:59

您不能简单地将数据库中的表添加到模型中,然后在模型中创建新的关联。默认情况下,它使用独立关联,该关联必须映射到其数据库对应项=该关系也必须存在于数据库中。您必须 将您的关系建模为 FK 关联,但它仅允许一对一和一对多关联。 此处描述了关联类型之间的差异

You cannot simply add tables from the database into your model and then create a new association in the model. By default it uses independent association which must be mapped to its database counterpart = the relation must exists in the database as well. You must model your relation as FK association but it allows only one-to-one and one-to-many associations. Differences between association types are described here.

萝莉病 2024-11-15 12:09:59

当我在更改连接字符串后最终“从数据库更新模型”时,我遇到了这个问题。

通过右键单击实体并打开和关闭某个键,它似乎导致了刷新并解决了问题。这看起来更像是实体框架的一个错误。

应该注意的是,在本例中我使用的是 MySQL 连接器,所以我怀疑它总体上相当挑剔。

I've had this problem when i ended up 'updating model from database' after having changed the connection string.

By right clicking the entity and toggling a key on and off It seems to have caused a refresh and has fixed the problem. This seems more like a bug with the Entity framework.

It should be noted in this case i was using the MySQL connector so I suspect it's just rather finicky in general.

衣神在巴黎 2024-11-15 12:09:59

如果模型上有不可映射的属性,您可能会收到此错误。

例如,我将一些 Linq2Sql 转换为 EF6,并且在 Binary 字段上出现错误。 Binary 是 System.Data.Linq 类型,但对于 EF,它需要是 byte[]。更改它解决了问题。

You can get this error if you have a property on the model that isn't mappable.

For instance I was converting some Linq2Sql over to EF6 and had an error on a Binary field. Binary is a System.Data.Linq type but for EF it needs to be byte[]. Changing it fixed the problem.

长途伴 2024-11-15 12:09:59

在 EF 6 中遇到了类似的问题:

exception.InnerException
{"\r\nEntities.EAM.msl(458,10) : error 3004: Problem in mapping fragments starting at line 458:No mapping specified for properties InspectionPdfReportRequest.SyncDate in Set InspectionPdfReportRequests.\r\nAn Entity with Key (PK) will not round-trip when:\r\n  Entity is type [Model.Entities.InspectionPdfReportRequest]\r\n\r\nEntities.EAM.msl(488,10) : error 3004: Problem in mapping fragments starting at line 488:No mapping specified for properties InspectionReportRequest.SyncDate in Set InspectionReportRequests.\r\nAn Entity with Key (PK) will not round-trip when:\r\n  Entity is type [Model.Entities.InspectionReportRequest]\r\n"}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146232032
    HelpLink: null
    InnerException: null

问题是我直接在文本编辑器中对数据库优先 EF 应用程序的 *.edmx 文件进行了更改,而不是使用设计器。因此,只需恢复文本更改并使用设计器应用它们,错误就会消失。在设计器中点击保存即可更新关联的 *.msl 文件。

Experienced a similar problem with EF 6:

exception.InnerException
{"\r\nEntities.EAM.msl(458,10) : error 3004: Problem in mapping fragments starting at line 458:No mapping specified for properties InspectionPdfReportRequest.SyncDate in Set InspectionPdfReportRequests.\r\nAn Entity with Key (PK) will not round-trip when:\r\n  Entity is type [Model.Entities.InspectionPdfReportRequest]\r\n\r\nEntities.EAM.msl(488,10) : error 3004: Problem in mapping fragments starting at line 488:No mapping specified for properties InspectionReportRequest.SyncDate in Set InspectionReportRequests.\r\nAn Entity with Key (PK) will not round-trip when:\r\n  Entity is type [Model.Entities.InspectionReportRequest]\r\n"}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146232032
    HelpLink: null
    InnerException: null

The problem turned out to be that I had made a change to the *.edmx file for my database-first EF application directly in a text editor instead of using the designer. So just reverted the text changes and applied them using the designer and the error went away. Hitting save in the designer is what updated the associated *.msl file.

一场春暖 2024-11-15 12:09:59

我从 Linqpad 收到此错误,无法弄清楚为什么它突然开始发生,以及这是否会成为我使用上下文的应用程序中的问题。我还刚刚删除了最近由新实体创建的类,然后右键单击 .tt 文件并点击“运行到光标”以重新生成该类。这为我解决了。

I was getting this error from Linqpad and could not figure out why it started happening all of a sudden and whether this would be an issue in my application using the context. I also just deleted the class that was recently created by a new entity and then I right clicked on the .tt file and hit "run to cursor" to regenerate the class. That fixed it for me.

月牙弯弯 2024-11-15 12:09:59

从数据库更新模型选项对我不起作用。

因此,我需要先删除所有实体,然后再从数据库更新模型才能成功修复解决方案。

Updating Model from Database option doesn't work for me.

So I need to delete all entities first before updating model from database to successfully fix the solution.

送舟行 2024-11-15 12:09:59

如果您已在实体上应用映射,请尝试从表中删除该列,这将解决问题

If you have applied a mapping on the entity, try to remove that column from the table which will resolve the issue

怪我鬧 2024-11-15 12:09:59

就我而言,我的数据库管理员已将列名称大写更改为小写
通过再次更新该表(从数据库更新模型)解决了该问题。然后,删除之前的大写列

In my case, my database administrator has changed column names from uppercase to lower.
I solved the problem by updating (update model from database) for that table again. Then, deleted previous uppercase columns.

最好是你 2024-11-15 12:09:59

就我而言,我重命名了表中的字段并添加了主键。之后,我得到了那个错误。我转到对象/模型列表,将其删除并从数据库刷新后,我再次遇到相同的错误。我尝试了几次但没有任何结果。 EF 生成的类是旧表结构和新表结构的混合体。

研究了一段时间,解决办法是这样的:
转到您的实体的图表。找到您的表格或在右侧打开模型资源管理器(右键单击图表 - 打开模型资源管理器)。在 ...Model、...Model.Store 下搜索您的模型/表并将其删除。之后,再次从数据库添加表,问题解决。

In my case, I had renamed a field in a table and added a primary key. After that, I got that error. I went to my list of objects/models and after delete it and refresh from the database, I had again the same error. I tried several times but nothing. The class that EF was generating was a mix between the old table structure and the new one.

After investigating for a while, this is the solution:
Go to the Diagram of your your entities. Locate your table or open on your right the Model Explorer (right click over the diagram - Open model explorer). Search for your model/table under ...Model, ...Model.Store and delete it. After that, add again the table from the database and problem solved.

谁的年少不轻狂 2024-11-15 12:09:59

就我而言,在解决方案中,首先我刷新了 MyProjectname.edmx 文件,但它不起作用。然后我删除了我修改过的表,并再次更新了模型表单数据库(不刷新它)。它起作用了。

in my case,in solution at first I refreshed MyProjectname.edmx file but it didn't work .Then I deleted the table that I had modified it and again updated the Model form database (not refresh it).And it works.

╰ゝ天使的微笑 2024-11-15 12:09:58

就我而言,不允许修改现有表,但我发现当您添加一个新表并在 EF4 中选中“在模型中包含外键列”并且该表不包含任何外键关系时,您会尝试添加关联的话就会触发这个错误。

在 EF4 模型中定义约束数据库中不存在


如果链接的文章消失,解决方案是:

您需要打开关联的属性窗口,然后单击引用约束省略号以获取引用约束对话框。然后为“从属属性”设置选择正确的字段。

In my case am not allowed to modify existing tables but I discovered that when you add a new table with "Include foreign key columns in the model" checked in EF4 and the table doesn't contain any foreign key relationships then you try to add a association it will trigger this error.

Defining Constraints in an EF4 model that don’t exist in the database


In case the linked article disappears, The solution is:

You need to open the properties window of the association and then click on the Referential Constraint ellipses to get to the ref constraint dialog. Then select the correct field for the 'Dependent Property' setting.

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