Linq to Entities, table per type and interchangeable foreign keys

I use Linq for objects using the Table per Type method. This happens very well, so far. I have the following setup:

  • Parent table
  • Children's table (inherits from the parent)
  • Large children's table (Inherited from the children's table)
  • Link to the table (has a hidden key from the foreign key to the table for children)

Here is the database diagram

alt text

Following the above video, I applied the Table Per Type approach to the default schema that Linq creates entities when adding the above tables to the model.

Before applying the table for each type:

alt text

After the table for type:

alt text

Then I compiled the project and got an error, which you can see in the image above. To fix this, I went over to matching for the link with the foreign key, I added the childid field, which the error message moaned about.

alt text

Then I recompiled and got another error:

The problem with displaying fragments on lines 147, 176: two objects with different keys are mapped to the same row. Make sure that these two display fragments do not display two groups of objects with overlapping keys to the same group of rows.

This is the moment when I am now. The problem is that the "ChildID" in the "LinkingTable" is Nullable. If I set it invalid, I will not get the above error.

I saved the database and project used in the steps above to sky drive .

Does anyone know how to fix this error?

Dave

Here's the fixed code (thanks The Gecko)

Before

<AssociationSetMapping Name="FK_LinkingTable_Child" TypeName="TablePerTypeModel.FK_LinkingTable_Child" StoreEntitySet="LinkingTable"> <EndProperty Name="Child"> <ScalarProperty Name="Id" ColumnName="ChildID" /> </EndProperty> <EndProperty Name="LinkingTable"> <ScalarProperty Name="LinkTableID" ColumnName="LinkTableID" /> </EndProperty> </AssociationSetMapping> 

After

 <AssociationSetMapping Name="FK_LinkingTable_Child" TypeName="TablePerTypeModel.FK_LinkingTable_Child" StoreEntitySet="LinkingTable"> <EndProperty Name="Child"> <ScalarProperty Name="Id" ColumnName="ChildID" /> </EndProperty> <EndProperty Name="LinkingTable"> <ScalarProperty Name="LinkTableID" ColumnName="LinkTableID" /> </EndProperty> <Condition ColumnName="ChildID" IsNull="false"/> </AssociationSetMapping> 
+7
linq linq-to-entities entity-framework table-per-type
source share
1 answer

Try updating the AssociationMapping node in the Mapping section of your EDMX file to include a condition for error.

eg.

 <AssociationSetMapping> ... <Condition ColumnName="" IsNull="false"/> </AssociationSetMapping> 
+5
source share

All Articles