Data Access has been discontinued. Please refer to this page for more information.

ForeignKeyAssociationAttribute

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

The ForeignKeyAssociationAttribute is used to designate a property to represent a one-to-many relationship (1:n relationship). The ForeignKeyAssociationAttribute derives from AssociationAttribute and exposes the following properties:

  • ConstraintName - represents the name of the foreign key constraint that defines the association. Can be null if there is no constraint in the database.
  • ForeignKeyColumns - represents a coma separated list of the foreign key columns.
  • SharedFields - represents the properties that share the same columns with the navigation member defined with the foreign key association.
  • TargetFields - represents the names of the properties on the opposite side of the relation.

You can encode as property references in your persistent class any relationships that will always be the same. In the Northwind sample database, for example, because products typically have suppliers, there is always a relationship in the model between the product and its supplier. Telerik Data Access defines a ForeignKeyAssociationAttribute attribute to help you represent such relationships. This attribute is used together with the StorageAttribute to represent what would be a foreign key relationship in the database. In the following one-to-one example, the Product class has a property that declares the relationship between a product and its supplier. The ForeignKeyAssociationAttribute attribute which marks the Supplier property describes how this association is accomplished. Namely, by specifying the ConstraintName, the foreign key column name (SharedFields) and the name of the column on the opposite side of the relation (TargetFields).

[Table( "Products" )]
public class Product
{
   private Supplier supplier;
   [Storage( "supplier" )]
   [ForeignKeyAssociation( ConstraintName = "FK_Products_Suppliers", 
   SharedFields = "SupplierId", TargetFields = "Id" )]
   public Supplier Supplier
   {
       get
       {
           return supplier;
       }
       set
       {
           supplier = value;
       }
   }
}
<Table("Products")>
Public Class Product
 Private _supplier As Supplier
 <Storage("supplier"), ForeignKeyAssociation(ConstraintName := "FK_Products_Suppliers", 
  SharedFields := "SupplierId", TargetFields := "Id")>
 Public Property Supplier() As Supplier
  Get
   Return _supplier
  End Get
  Set(ByVal value As Supplier)
   _supplier = value
  End Set
 End Property
End Class