LinqConnect Documentation
In This Topic
    Defining Relationships
    In This Topic

    This topic is based on: Creating Entity Classes.

    Relationships in relational databases are typically represented as foreign key values referring to primary keys in other tables. To navigate between them, you must use a relational join operation. Objects, on the other hand, refer to each other using property references or collections of references navigated using "dot" notation. LinqConnect defines an Association attribute you can apply to a member used to represent a relationship. An association relationship is one like a foreign-key to primary-key relationship that is made by matching column values between tables.

    To define a relationship use the EntitySet container in the parent entity.

    [Table(Name="Company")]
    public class Company
    {
       ...
       private EntitySet<Order> _Orders;
       [Association(Storage="_Orders", OtherKey="CompanyID")]
       public EntitySet<Order> Orders {
          get { return this._Orders; }
          set { this._Orders.Assign(value); }
       }
       ...
    }
    <Table(Name:="Company")> _
    Public Class Company
       ...
       Private _Orders As EntitySet(Of Order)
       <Association(Storage:="_Orders", OtherKey:="CompanyID")> _
       Public Property Orders() As EntitySet(Of Order)
         Get
            Return Me._Orders
         End Get
         Set(ByVal value As EntitySet(Of Order))
         End Set
       End Property
       ...
    End Class
    

    The Company class now has a property that declares the relationship between companies and their orders. The Orders property is of type EntitySet because the relationship is one-to-many. We use the OtherKey property of the Association attribute to describe how this association is done. It specifies the names of the properties in the related class to be compared with this one. There was also a ThisKey property we did not specify. Normally, we would use it to list the members on this side of the relationship. However, by omitting it we allow LinqConnect to infer them from the members that make up the primary key.

    Now take a look at the relationship declaration at the Order class side.

    [Table(Name="Orders")]
    public class Order
    {
       ...
       [Column]
       public int CompanyID;
       
       private EntityRef<Company> _Company;   
       [Association(Storage="_Company", ThisKey="CompanyID")]
       public Company Company {
          get { return this._Company.Entity; }
          set { this._Company.Entity = value; }
       }
       ...
    }
    <Table(Name:="Orders")> _
    Public Class Order
       ...
       <Column> _
       Public CompanyID As Integer
       
       Private _company As EntityRef(Of Company)
       <Association(Storage:="_Company", ThisKey:="CompanyID")> _
       Public Property company() As company
         Get
           Return Me._Company.Entity
         End Get
         Set(ByVal value As Company)
           Me._Company.Entity = value
         End Set
       End Property
       ...
    End Class
    

    The Order class uses the EntityRef type to describe the relationship back to the company. EntityRef class should be used to support deferred loading. The Association attribute specifies the ThisKey property for the Company property since the non-inferable members are now on this side of the relationship.

    Also pay attention to the Storage property. It tells LinqConnect which private member is used to hold the value of the property. This allows LinqConnect to bypass your public property accessors when it stores and retrieves their value to avoid any custom business logic. If the storage property is not specified, the public accessors will be used instead. You may use the Storage property with Column attributes as well.

    This topic is continued in: The DataContext.