Entity Framework 4 mapping fragment error when adding new entity scalar
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?
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.
I just removed the offending tables from the model and then added them back and all was well.
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.
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