Up Create the entity data model
Model » Create the entity data model » Foreign Key Associations required

Foreign Key Associations required

Last modified on September 14, 2012 14:55

Entity Framework supports two kinds of associations: "Independent" and "Foreign Key". All associations in a DevForce model must be "Foreign Key Associations".


Foreign Key Associations only

The Entity Framework supports two types of association, "Foreign Key Associations" and "Independent Associations". 

"Independent" associations were the only type of association in version 1 of EF. "Independent" associations hide their foreign keys and they lack a "Referential Constraint" section in their XML definitions.

Here's an example:

XML
<Association Name="CustomerOrder">
    <End Type="DomainModel.Customer" Role="Customer" Multiplicity="1" />
    <End Type="DomainModel.Order" Role="Order" Multiplicity="*" />
</Association>

DevForce cannot work with these key-less "Independent" associations. All associations must be the "Foreign Key" type that was introduced in EF version 4. "Foreign Key" associations expose their foreign keys and have a "Referential Constraint" section as seen in this EDMX fragment.

XML
<Association Name="FK_Order_Customer">
   <End Type="DomainModel.Customer" Role="Customer" Multiplicity="0..1" />
   <End Type="DomainModel.Order" Role="Order" Multiplicity="*" />
   <ReferentialConstraint>
     <Principal Role="Customer">
      <PropertyRef Name="CustomerID" />
     </Principal>
     <Dependent Role="Order">
       <PropertyRef Name="CustomerID" />
     </Dependent>
   </ReferentialConstraint>
<Association>

Check "Include foreign key columns" in the EDM Wizard

When you first created your EDM with the EDM Wizard and chose the "Data First" modeling approach, you were required to check the CheckBox next to "Include foreign key columns".

"Include foreign key columns" is especially critical. If you uncheck the option by accident, the "Update Model from Database" wizard creates new associations as "Independent" associations. You have to fix that.

You must restore the "Foreign Key" option. You can turn it back on in the "Update Model from Database..." wizard while adding a new object to the model

Or you can add the option in XML by editing the EDMX.

  • Open EDMX file in the "XML (Text) Editor"
  • Locate the EF Designer section (it's the last section)
  • Locate the "<Options>" subsection (near the top of the EF Designer section)
  • Add the "IncludeForeignKeysInModel" designer property as seen here
XML
    <Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
        <DesignerProperty Name="EnablePluralization" Value="True" />
        <DesignerProperty Name="IncludeForeignKeysInModel" Value="True" />
      </DesignerInfoPropertySet>
    </Options>
  • Save and close the EDMX editor

You have to manually fix all associations that you created before re-engaging "Foreign Key Associations" as discussed in the "Model First" topic

Converting to a Foreign Key Association

You must convert all existing "Independent" association to a "Foreign Key" association by (a) adding the foreign key properties manually and (b) adding a "Referential Constraint". 

Open the "Referential Constraint" dialog either by double-clicking the Association line in the diagram or pressing the button in the "Referential Constraint" property.

EdmDesignerRefConstraintProperty.png

Then fill in the blanks as in this example:

EdmDesignerRefConstraintDialog.png

Tags:
Created by DevForce on March 20, 2011 13:45

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade