52

Error 3007: Problem in Mapping Fragments starting at lines 186, 205: Non-Primary-Key column(s) [WheelID] are being mapped in both fragments to different conceptual side properties - data inconsistency is possible because the corresponding conceptual side properties can be independently modified.

I found several places on the web describing this error, but I simply don't understand them. (confused smiley goes here)

One
Two
Three
Four

There is something pretty fundamental here, I must be missing. Can you explain it, so that I understand it? Maybe using my real life example below?

alt text

Foreign key 1:N Wheels.Id -> Slices.WheelId

I add them to entity framework, and WheelId is not visible in the Slices-entity.

alt text

Doing some workaround (deleting the relationship from the db before adding tables to EF - then re-creating it and updating EF) I managed to get the WheelId to stay in Slices, but then I get the error mentioned at the top.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Kjensen
  • 12,447
  • 36
  • 109
  • 171

12 Answers12

44

Since Slices.WheelId is an FK, you cannot expose it in your client model, period. There are ways to get the value, though.

var wheelId = someSlice.Wheels.ID;

Update In EF 4 you can do this by using FK Associations instead of independent associations.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • What if I need to update the `WheelId` in the `Slices` table? For example, if the user needs to change the wheels on a particular slice, what needs to be done? – Justin T Conroy Jul 17 '12 at 23:03
  • You find the new `Wheel` and assign it to the slice: `someSlice.Wheels = someWheel;` – Craig Stuntz Jul 18 '12 at 13:18
27

Try to remove foreign property column from Entity set using entity model design it will solve your problem

For example

We have two tables one is customer and other one is order, using entity model design we added association between customers and orders when we do this Ado.net entity framework i will add navigation properties to both below tables.

Like Customer.Orders - Here order is list Order.Customer

One - Many relation.

So we need to remove property from with name CustomerId[Foreign key column] from Order entity set.

For reference:

http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/2823634f-9dd1-4547-93b5-17bb8a882ac2/

Venkat
  • 868
  • 1
  • 12
  • 21
9

I was able to overcome this problem by the following steps: right click the designer window Select 'update model from database' Select Add AND make sure that the 'Include foreign key columns in the model' checkbox is selected. click on Finish...

jahansha
  • 186
  • 1
  • 3
3

I had set foreign keys up in the database but framework still wasn't pulling them in correctly. So I tried to add the association myself. 

However, when I did this I would get a mapping error. It took me A WHILE but I figured out. What I did was set up the association using the entity toolbox association tool and then you have to double click on the association (1 to many) line and set the primary and foreign key there. Hopefully, this to help others who might have the same problem. I couldn't find the answer anywhere.

2

I had this problem for quite a different reason, and the message was slightly different; it didn't say "data inconsistency is possible because the corresponding conceptual side properties can be independently modified."

I have a table involved in my model with a binary column where I store image data. I only want this data returned when I need it (performance is a feature), so I split the table using a method similar to this. Later on, I added a property to that table, then updated the model from the database. The wizard added the property to both entity types that refer to the table with the added property. I had to delete it from one of them to solve the error.

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
1

I've had this happen because Entity Framework Update wizard mismapped some keys (or did not update?). As a result, some columns were mistakenly labeled as keys, while actual key columns were treated as plain columns.

The solution was to manually open EDMX file, find the entities, and update the keys.

ikh
  • 2,336
  • 2
  • 19
  • 28
  • 1
    I have changend some keys (Relationships) in the database and got a variant of the the mentioned error after trying ot update the EDMX model. My (probably overshooting) solution was to just delete and recreate the model. – Marcel Mar 23 '15 at 10:14
1

Couldn't get any of the answer to work with EF6. The problem seems to be the framework doesn't import the foreign keys correctly as Associations. My solution was removing foreign keys from the tables, and then manually adding the associations using Entity Framework model, using the following steps: Entity Framework - Add Navigation Property Manually

Community
  • 1
  • 1
Eternal21
  • 4,190
  • 2
  • 48
  • 63
1

in my case I solved this error by tick (include foreign key columns in the model)

- update Model from database
- tick (include foreign key columns in the model)
- finish 

enter image description here

Majid Omar
  • 51
  • 4
0

For LinQ to Entities queries in EF1, my workaround for not having access to the foreign key as a property is with the following code, which does not produce a join query to the associated table:

dbContext.Table1s.FirstOrDefault(c => (int?)c.Table2.Id == null)

i.e, the generated SQL is:

...WHERE ([Extent1].[Table2Id] IS NULL)...
InActive
  • 822
  • 6
  • 5
0

Solution is to allow deleting Rule = Cascade on Sql association.

Same thing as to be done on .edmx model, adding element to association:

<Association Name="FK_Wheels_Slices">
          <End Role="Wheels" Type= "your tipe here" Multiplicity="1">
          <OnDelete Action="Cascade" />
          </End>
 </Association>
sharkbait
  • 2,980
  • 16
  • 51
  • 89
0

I had a table already mapped in EF. I added two more tables which had foreign keys in the previously added table. I then got the 3007 error.

To fix the error I deleted all three tables from the EDMX file, and then re-added them all at once together (via "Update Model from Database..."), instead of in stages.

Daren
  • 139
  • 4
  • 5
0

I checked my Error List window and noticed I had errors in the model. Fixed them and all is well

dangalg
  • 6,398
  • 4
  • 27
  • 37