views:

123

answers:

1

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.

+2  A: 

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