Entity-framework – Problem in mapping fragments in Entity Framework

entity-frameworkentity-framework-4entity-framework-designerentity-relationship

I am using entity framework and I ran into an odd build error.

I am building a forum and I set up a table in the database for "ignores" when people don't like each other they will ignore someone. The table has two columns and together they are the primary keys.

PK InitiatingUser
PK IgnoredUser

When EF maps this table I get this error:

Error 7 Error 3034: Problem in mapping fragments starting at lines 1467, 1477:Two entities with possibly different keys are mapped to the same row. Ensure these two mapping fragments map both ends of the AssociationSet to the corresponding columns.

I opened up the edmx in the xml editor and navigated to the offending lines.

          <MappingFragment StoreEntitySet="Ignores">
            <ScalarProperty Name="IgnoredUser" ColumnName="IgnoredUser" />
            <ScalarProperty Name="InitiatingUser" ColumnName="InitiatingUser" />
          </MappingFragment>

I am just getting started with EF and I don't understand what is going on or what the issue might be.

Any help is appreciated.

EDIT
The relationships between ignores used to have foreign keys mapping both initiating user and ignored user to the primary key (username) of users table. That was how it was when I first mapped EF to this table. I have since deleted the FKs to see if that would help but it didn't.

Best Answer

This is likely due to including a many-to-many join table in your entity model, or what EF thinks is such a table (possibly such as one that doesn't have its own self-contained key, but whose identity is made up of two or more foreign keys).

So, for example, let's say you have the following tables:

  • Person
  • Address
  • PersonAddress (contains only PersonID and AddressID)

In your entity model, you should only add Person and Address. If you add PersonAddress, then EF will throw the error. According to this MSDN Q&A, EF will take the join table into account automatically.

Related Topic