database 错误 3004:从行开始映射片段的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6063701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:11:53  来源:igfitidea点击:

Error 3004: Problem in mapping fragment starting at line

databaseentity-frameworkmappingentity-relationshipedmx

提问by Steve Kershaw

I have this error when I build my Web Service:

我在构建 Web 服务时遇到此错误:

Error 3004: Problem in mapping fragment starting at line 323: No mapping specified for properties JE_TRN_HS.JE_HDR_HSJE_HDR_KEY. Entity is type [TESTCPModel.JE_TRN_HS

错误 3004:从第 323 行开始的映射片段有问题:没有为属性 JE_TRN_HS.JE_HDR_HSJE_HDR_KEY 指定映射。实体类型为 [TESTCPModel.JE_TRN_HS

This is what happend. I created this Entity Data Model from an existing database. I added 2 tables and rebuilt the Web Service. The build was successfull.

这就是发生的事情。我从现有数据库创建了这个实体数据模型。我添加了 2 个表并重建了 Web 服务。构建成功。

THEN I added an Association between the two tables. When I tried to rebuild it failed with the error message displayed above!

然后我在两个表之间添加了一个关联。当我尝试重建它失败并显示上面显示的错误消息时!

Now here's the kicker! I deleted the newly added Association and rebuilt. The build failed with the same error message!? The only way I can get rid of this error is if I delete the second table, rebuild and re-add the second table??

现在是踢球者!我删除了新添加的关联并重建。构建失败并显示相同的错误消息!?我可以摆脱这个错误的唯一方法是删除第二个表,重建并重新添加第二个表?

I've looked EVERYWHERE for the solution to this problem! Thanks Steve

我到处寻找这个问题的解决方案!谢谢史蒂夫

回答by Carlos

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.

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

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

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



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.

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

回答by Graham Laight

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.

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

回答by Ladislav Mrnka

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 associationbut it allows only one-to-one and one-to-many associations. Differences between association types are described here.

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

回答by Leniel Maccaferri

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

我现在遇到了这个问题......需要添加一个标量属性,让我们说custom_property一个使用 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_propertycolumn that got added to the table named custom_table.

我可以从数据库更新模型,但这不是一个选项。它在模型中导致了很多错误。这是一个巨大的数据库模型,我需要添加此属性的表可能有 30 多个关系。我只是想映射这个custom_property添加到名为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}
  • InnerException {"\r\nModels.MyDB.msl(352,10) : 错误 3004:从第 352 行开始映射片段时出现问题:未为 Set custom_table 中的属性 custom_table.custom_property 指定映射。\r\n带有键的实体 (PK) ) 在以下情况下不会往返:\r\n 实体类型为 [MyDB.custom_table]\r\n"} System.Exception {System.Data.Entity.Core.MappingException}

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

Error ListVS 这被显示:Error 11009: Property ' ' is not mapped

Steps I took to fix this:

我采取的步骤来解决这个问题:

  1. Edited the .edmxfile in Visual Studio Code;
  2. Looked for custom_table;
  3. Complemented the .edmxXML code with mappings.
  1. .edmx在 Visual Studio Code 中编辑文件;
  2. 寻找custom_table;
  3. .edmx用映射补充了XML 代码。

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>

回答by ali mahdian

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.

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

回答by Highstead

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.

应该注意的是,在这种情况下,我使用的是 MySQL 连接器,所以我怀疑它通常很挑剔。

回答by Simon_Weaver

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

如果模型上有不可映射的属性,则会出现此错误。

For instance I was converting some Linq2Sqlover to EF6and had an error on a Binaryfield. Binary is a System.Data.Linqtype but for EF it needs to be byte[]. Changing it fixed the problem.

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

回答by user8128167

Experienced a similar problem with EF 6:

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

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

回答by spatemp

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.

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

回答by Willy David Jr

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.

因此,在从数据库更新模型以成功修复解决方案之前,我需要先删除所有实体。