Defining an Entity Framework 1:1 association

18,755

You have to select the table, that cotains the foreign key. The next step is to remove that foreign key from the entity, as it is already expressed using the relation you just created.

Example:

table A
-------
A_ID int
B_ID int


table B
-------
B_ID int

In this case, you would select the table A in the designer, as it contains the foreign key. Also you'll need to remove B_ID from the A entity afterwards.

Share:
18,755
Craig Fisher
Author by

Craig Fisher

Transplanted Aussie moved to Seattle in 1998 to work for Microsoft. I spent 8.5 years working on the Commerce Server (2000 & 2002), Data Protection Manager and Speech Server products. Very familiar with internet software development and expert in software globalization. Currently working at EagleView Technologies.

Updated on June 05, 2022

Comments

  • Craig Fisher
    Craig Fisher almost 2 years

    I'm trying to define a 1:1 association between two entities (one maps to a table and the other to a view - using DefinedQuery) in an Entity Framework model. When trying to define the mapping for this in the designer, it makes me choose the (1) table or view to map the association to. What am I supposed to choose? I can choose either of the two tables but then I am forced to choose a column from that table (or view) for each end of the relationship. I would expect to be able to choose a column from one table for one end of the association and a column from the other table for the other end of the association, but there's no way to do this.

    Here I've chosen to map to the "DW_ WF_ClaimInfo" view and it is forcing me to choose two columns from that view - one for each end of the relationship.

    I've also tried defining the mapping manually in the XML as follows:

    <AssociationSetMapping Name="Entity1Entity2" TypeName="ClaimsModel.Entity1Entity2"
         StoreEntitySet="Entity1">
      <EndProperty Name="Entity2">
        <ScalarProperty Name="DOCUMENT" ColumnName="DOCUMENT" />
      </EndProperty>
      <EndProperty Name="Entity1">
        <ScalarProperty Name="PK_DocumentId" ColumnName="PK_DocumentId" />
      </EndProperty>
    </AssociationSetMapping>
    

    But this gives: Error 2010: The Column 'DOCUMENT' specified as part of this MSL does not exist in MetadataWorkspace. Seems like it still expects both columns to come from the same table, which doesn't make sense to me.

    Furthermore, if I select the same key for each end, e.g.:

    <AssociationSetMapping Name="Entity1Entity2" TypeName="ClaimsModel.Entity1Entity2"
         StoreEntitySet="Entity1">
      <EndProperty Name="Entity2">
        <ScalarProperty Name="DOCUMENT" ColumnName="PK_DocumentId" />
      </EndProperty>
      <EndProperty Name="Entity1">
        <ScalarProperty Name="PK_DocumentId" ColumnName="PK_DocumentId" />
      </EndProperty>
    </AssociationSetMapping>
    

    I then get:

    Error 3021: Problem in Mapping Fragment starting at line 675: Each of the following
    columns in table AssignedClaims is mapped to multiple conceptual side properties:
      AssignedClaims.PK_DocumentId is mapped to
        <AssignedClaimDW_WF_ClaimInfo.DW_WF_ClaimInfo.DOCUMENT,
        AssignedClaimDW_WF_ClaimInfo.AssignedClaim.PK_DocumentId>
    

    What am I not getting?