Creating an Entity Framework 1:1 relationship

.net ado.net entity-framework visual-studio

Question

In an Entity Framework model, I'm attempting to build a 1:1 relationship between two entities (one maps to a table and the other to a view using DefinedQuery). It forces me to choose the (1) table or view to map the association to when I attempt to create the mapping for this in the designer. What am I expected to select? The only restriction is that I must choose a column from that table (or view) at either end of the connection, even though I may select any of the two tables. There is no ability to pick a column from one table for one end of the association and another column from a different table for the other end, as I would have expected.

The "DW_ WF ClaimInfo" view is the one I've decided to map to in this case, which forces me to choose two columns from it—one for either end of the connection.

Additionally, I tried manually specifying the mapping 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 results: The Column 'DOCUMENT' mentioned as a part of this MSL does not exist in MetadataWorkspace, according to error code 2010. I don't see why it still assumes that both columns would originate from the same table.

Additionally, if I choose the same key on each end, for instance:

<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>

Then I 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 missing?

1
12
9/7/2014 12:06:54 PM

Popular Answer

You have to select the table, that cotains the foreign key. As the foreign key is already stated using the relation you just made, the next step is to delete it from the entity.

Example:

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


table B
-------
B_ID int

Given that table A has the foreign key in this situation, you should choose it from the designer. Additionally, you must afterwards delete B ID from the A entity.

6
1/24/2009 4:34:04 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow