.net 添加新实体标量时实体框架 4 映射片段错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3002082/
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-03 14:23:01  来源:igfitidea点击:

Entity Framework 4 mapping fragment error when adding new entity scalar

.netentity-frameworkentity-framework-4

提问by Jason Morse

I have an Entity Framework 4 model-first design. I create a first draft of my model in the designer and all was well. I compiled, generated database, etc.

我有一个实体框架 4 模型优先设计。我在设计师中创建了我的模型的初稿,一切都很好。我编译,生成数据库等。

Later on I tried to add a string scalar (Nullable = true) to one of my existing entities and I keep getting this type of error when I compile:

后来我尝试向我现有的实体之一添加一个字符串标量(Nullable = true),并且在编译时不断收到此类错误:

Error 3004: Problem in mapping fragments starting at line 569: No mapping specified for properties MyEntity.MyValue in Set MyEntities. An Entity with Key (PK) will not round-trip when: Entity is type [MyEntities.MyEntity]

错误 3004:从第 569 行开始映射片段时出现问题:未为 Set MyEntities 中的属性 MyEntity.MyValue 指定映射。在以下情况下,具有密钥 (PK) 的实体不会往返: 实体类型为 [MyEntities.MyEntity]

I keep having to manually open the EDMX file and correct the XML whenever I add scalars.

每当我添加标量时,我都不得不手动打开 EDMX 文件并更正 XML。

Ideas on what's going on?

关于发生了什么的想法?

回答by Jason Morse

Have since discovered that after I add/change/delete properties on my entities I must "Generate Database from Model" before I compile otherwise I get 3004 mapping errors.

从那以后发现,在我添加/更改/删除实体上的属性后,我必须在编译之前“从模型生成数据库”,否则我会收到 3004 映射错误。

回答by avenue19

I just removed the offending tables from the model and then added them back and all was well.

我只是从模型中删除了有问题的表格,然后将它们添加回来,一切都很好。

回答by Jeff Johnson

For those of you who are creating a model from a database, I had this issue after I made changes to my DB. It happened when I changed a field name in the DB for one reason or another (I think it also happens if you change a data type).

对于那些从数据库创建模型的人,我在更改数据库后遇到了这个问题。当我出于某种原因更改数据库中的字段名称时会发生这种情况(我认为如果更改数据类型也会发生这种情况)。

The solution, for me, was to right-click on the workspace and choose "Update Model from Database". This should add the properties from the DB to your model, however, it does NOT remove your old properties and those will give you the 3004 error.

对我来说,解决方案是右键单击工作区并选择“从数据库更新模型”。这应该将数据库中的属性添加到您的模型中,但是,它不会删除您的旧属性,并且这些属性会给您 3004 错误。

Right-click on the workspace and choose "Validate". This should give you an error list showing the offending properties. You can then right-click each offending property and delete it manually from your model.

右键单击工作区并选择“验证”。这应该会给你一个错误列表,显示有问题的属性。然后,您可以右键单击每个有问题的属性并从模型中手动删除它。

This fixed the issue for me. Hope it helps someone else.

这为我解决了这个问题。希望它可以帮助别人。

回答by Phil

If you want to modify your database without regenerating the entire model or recreating your database from the model, I find it's easiest and safest to amend the properties in the EDMX diagram via Visual Studio and then manually adjust the mappings which visual studio doesn't give access to.

如果你想修改你的数据库而不重新生成整个模型或从模型重新创建你的数据库,我发现通过 Visual Studio 修改 EDMX 图中的属性,然后手动调整 Visual Studio 没有提供的映射是最简单和最安全的进入。

The error will give you a line number:

错误会给你一个行号:

Problem in mapping fragments starting at line 569

从第569行开始映射片段的问题

Just open the edmx file in a text editor, go to that line and it should be quite obvious what needs fixing. There's a section which will look like this:

只需在文本编辑器中打开 edmx 文件,转到该行,应该很明显需要修复什么。有一个部分看起来像这样:

<EntityTypeMapping TypeName="YourModel.YourType">
  <MappingFragment StoreEntitySet="YourType">
    <ScalarProperty Name="PropertyName1" ColumnName="DatabaseColumn1" />
    <ScalarProperty Name="PropertyName2" ColumnName="DatabaseColumn2" />
    ...
  </MappingFragment>
</EntityTypeMapping>

Just make sure there's a node for each property/column name you need, and that all properties are also listed in the <EntityType Name="YourTable">section at the top of the edmx document

只需确保您需要的每个属性/列名称都有一个节点,并且所有属性也列在<EntityType Name="YourTable">edmx 文档顶部的部分中

回答by Faiyaz

Another alternative to do so without removing table from diagram and re-adding it, is by using Table Mapping option.

另一种不从图表中删除表格并重新添加它的替代方法是使用表格映射选项。

  1. Goto to the model browser
  2. Right click on table name.
  3. Choose tablle mapping
  4. Visual Studio will show a window with EF properties matching/missing table columns.
  5. Select/match appropriate column to missing property
  1. 转到模型浏览器
  2. 右键单击表名。
  3. 选择表格映射
  4. Visual Studio 将显示一个窗口,其中包含匹配/缺少表列的 EF 属性。
  5. 选择/匹配合适的列到缺失的属性

enter image description here

在此处输入图片说明

回答by Faiyaz

If all your queries are written in stored procedures and you are just trying to fill the model then you could switch to a complex type which worked for me.

如果您的所有查询都是在存储过程中编写的,而您只是想填充模型,那么您可以切换到对我有用的复杂类型。

How to create a complex type:

如何创建复杂类型:

  1. Manually change the name of the Entity Generated class you are having issues with to a name not already in use in your edmx file.
  2. Select the Model Browser window and right-click/add a new complex type.
  3. Copy/Paste the properties of the original model to the complex type.
  4. Map your stored procedures to the complex type.

    I hope this helps someone.

  1. 手动将您遇到问题的实体生成类的名称更改为您的 edmx 文件中尚未使用的名称。
  2. 选择模型浏览器窗口并右键单击/添加一个新的复杂类型。
  3. 将原始模型的属性复制/粘贴到复杂类型。
  4. 将您的存储过程映射到复杂类型。

    我希望这可以帮助别人。

回答by Zlazher

I received the same problem after renaming a property on one of my entities.

在我的一个实体上重命名属性后,我收到了同样的问题。

I found out that the mapping between the property of my entity and the corresponding column in the table was not set.

我发现我的实体的属性和表中相应列之间的映射没有设置。

You can set this using the editor by right clicking on the entity in the model and choosing "Table mapping". Make sure the properties are mapped to a column in the database.

您可以使用编辑器通过右键单击模型中的实体并选择“表映射”来设置它。确保属性映射到数据库中的列。

Edit: I also needed to re-run my tt file to generate the new classes. (Might have been since I have it in another project?)

编辑:我还需要重新运行我的 tt 文件来生成新类。(可能是因为我在另一个项目中拥有它?)

回答by Darío León

After review the xml file (edmx), move the model to another project and realize that all is ok, I decided to close and open the Visual Studio and complile againt, then error disapears.

查看 xml 文件 (edmx) 后,将模型移至另一个项目并意识到一切正常,我决定关闭并打开 Visual Studio 并再次编译,然后错误消失。