Entity Framework 4 mapping fragment error when adding new entity scalar

.NetEntity FrameworkEntity Framework-4

.Net Problem Overview


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.

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:

> 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]

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

Ideas on what's going on?

.Net Solutions


Solution 1 - .Net

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.

Solution 2 - .Net

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

Solution 3 - .Net

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.

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.

Solution 4 - .Net

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.

The error will give you a line number:

> Problem in mapping fragments starting at line 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:

<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

Solution 5 - .Net

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.

Solution 6 - .Net

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

enter image description here

Solution 7 - .Net

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?)

Solution 8 - .Net

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJason MorseView Question on Stackoverflow
Solution 1 - .NetJason MorseView Answer on Stackoverflow
Solution 2 - .Netavenue19View Answer on Stackoverflow
Solution 3 - .NetJeff JohnsonView Answer on Stackoverflow
Solution 4 - .NetPhilView Answer on Stackoverflow
Solution 5 - .Netuser297691View Answer on Stackoverflow
Solution 6 - .NetFaiyazView Answer on Stackoverflow
Solution 7 - .NetZlazherView Answer on Stackoverflow
Solution 8 - .NetDarío LeónView Answer on Stackoverflow