3

I have two tables: Address and Contact which are joined on contactID (in Contact). Both of these tables have entities in my Entity data model (EF 4.0) and I do not wan't to modify them.

I do want to create a new entity that contains information from both entities.

What I did so far:

In CSDL:

<EntityContainer...>
    <EntitySet Name="AddressTest" EntityType="WebGearsModel.Test" />
    <EntitySet Name="ContactTest" EntityType="WebGearsModel.Test" />
</EntityContainer>

<EntityType Name="Test">
  <Key>
    <PropertyRef Name="addressID" />
  </Key>
  <Property Type="Int32" Name="addressID" Nullable="false" annotation:StoreGeneratedPattern="Identity"  />
  <Property Type="Int32" Name="contactID" Nullable="false"  />
  <Property Type="String" Name="firstName" Nullable="false" MaxLength="30" FixedLength="false" Unicode="false" />
  <Property Type="String" Name="emailAddress" Nullable="false" MaxLength="150" FixedLength="false" Unicode="false" />
</EntityType>

In my C-S mapping:

<EntitySetMapping Name="AddressTest">
  <EntityTypeMapping TypeName="WebGearsModel.Test">
    <MappingFragment StoreEntitySet="Address">
      <ScalarProperty Name="addressID" ColumnName="addressID" />
      <ScalarProperty Name="contactID" ColumnName="contactID" />
      <ScalarProperty Name="firstName" ColumnName="firstName" />
    </MappingFragment>
  </EntityTypeMapping>
</EntitySetMapping>

<EntitySetMapping Name="ContactTest">
  <EntityTypeMapping TypeName="WebGearsModel.Test">
    <MappingFragment StoreEntitySet="Contact">
      <ScalarProperty Name="contactID" ColumnName="contactID" />
      <ScalarProperty Name="emailAddress" ColumnName="emailAddress" />
    </MappingFragment>
  </EntityTypeMapping>
</EntitySetMapping>

The error I'm receiving is:

Problem in mapping fragments starting at line 150:Must specify mapping for all key properties (ContactTest.addressID) of the EntitySet ContactTest.

How am I supposed to map an AddressID from the Contact entity when it doesn't exist in that entity? I guess I need some sort of association but I'm unsure how to go about it... Remember, I don't want to have to modify my existing Address and Contact entities.

Chris Klepeis
  • 9,783
  • 16
  • 83
  • 149

1 Answers1

4

Remember the definition of an Entity:

An object that is not defined by its attributes, but rather by a thread of continuity and its identity.

Every "entity" must have something that uniquely identifies it; a key. However, you appear to be trying to define two types of entities from a single physical type that has only one key that provides a consistent identity for addresses, but not contacts. That violates the rules of an Entity, making the ContactTest concept invalid.

Since the underlying physical type, Test, defines a key property, addressID, all EntitySet's derived from that type must map that property to conform to the rules defining an Entity. Maintaining consistency of state is impossible otherwise.

jrista
  • 32,447
  • 15
  • 90
  • 130
  • i have this same issue, but i do not understand any of the above, is there a lamens definition of this for someone new to entity framework? Thanks – AlexW Jul 16 '13 at 08:51
  • Well, this was an answer to a much older version of EF...back in the 1.0 days. There have been a number of chances since then, so I do not know if this still applies, but the point is that in order to combine the Address and Contact, both entities must contain the same key. In the OP case, Address was the entity that had the primary key, addressID. To directly concatenate Contact to Address, the Contact entity, as well as its underlying table, must also have an addressID. EF can then properly associate the two halves of the entity via their common key...addressID. – jrista Jul 16 '13 at 19:41