LinqConnect Documentation
In This Topic
    Association Mapping
    In This Topic

    LinqConnect allows you to set the following parameters for mapped association:

    Property Type Description
    Name String The name of the association. This is often the same as the database's foreign-key constraint name. It is used to help to distinguish between multiple relationships in a single entity class referring to the same target entity class. Relationship properties on both sides of the relationship (if both are defined) must have the same name.
    Storage String The name of the underlying storage member. If specified, it tells LinqConnect how to bypass the public property accessor for the data member and interact with the raw value itself. If not specified LinqConnect gets and sets the value using the public accessor. It is recommended that all association members be properties with separate storage members identified.
    ThisKey String A comma-separated list of names of one or more members of this entity class that represent the key values on this side of the association. If not specified, the members are assumed to be the members that make up the primary key.
    OtherKey String A comma-separated list of names of one or more members of the target entity class that represent the key values on the other side of the association. If not specified, the members are assumed to be the members that make up the other entity class's primary key.
    IsUnique Boolean True if there a uniqueness constraint on the foreign key, indicating a true 1:1 relationship. This property is seldom used as 1:1 relationships are nearly impossible to manage within the database. Mostly entity models are defined using 1:n relationships even when they are treated as 1:1 by application developers.
    IsForeignKey Boolean True if the target "other" type of the association is the parent of the source type. With foreign-key to primary-key relationships, the side holding the foreign-key is the child and the side holding the primary key is the parent.
    DeleteRule String Used to add delete behavior to this association. For example, "CASCADE" would add "ON DELETE CASCADE" to the FK relationship. If set to null, no delete behavior is added.
    LinkTableName String Used for many-to-many associations. Defines the name of the many-to-many association table.
    LinkThisKey String Used for many-to-many associations. A comma-separated list of names of one or more columns of the intermediate many-to-many association table, corresponding to ThisKey members of this entity class.
    LinkOtherKey String Used for many-to-many associations. A comma-separated list of names of one or more columns of the intermediate many-to-many association table, corresponding to OtherKey members of the target entity class.

    Attribute Mapping

    Association attribute is used for defining an association.


    C#csharpCopy Code
        [Devart.Data.Linq.Mapping.Table(Name = "Orders")]
        public partial class Order
        {
            [Devart.Data.Linq.Mapping.Column(IsPrimaryKey = true)]
            public int OrderID;
     
            [Devart.Data.Linq.Mapping.Column]
            public int CompanyID;
     
            [Devart.Data.Linq.Mapping.Association(Name = "Company_Order", Storage = "_Company", ThisKey = "CompanyID", OtherKey = "CompanyID", IsForeignKey = true)]
            public Company Company
            {
                get { return this._Company.Entity; }
                set { this._Company.Entity = value; }
            }
     
            private Devart.Data.Linq.EntityRef<Company> _Company;
     
            public Order()
            {
                this._Company = default(EntityRef<Company>);
            }
        }
     
        [Devart.Data.Linq.Mapping.Table(Name = "Companies")]
        public partial class Company
        {
            [Devart.Data.Linq.Mapping.Column(IsPrimaryKey = true)]
            public int CompanyID;
     
            [Devart.Data.Linq.Mapping.Association(Name = "Company_Order", Storage = "_Orders",ThisKey = "CompanyID", OtherKey = "CompanyID")]
            public Devart.Data.Linq.EntitySet<Order> Orders
            {
                get { return this._Orders; }
                set { this._Orders.Assign(value); }
            }
     
            private Devart.Data.Linq.EntitySet<Order> _Orders;
            
            public Company()
            {
                this._Orders = new EntitySet<Order>();
            }
     
        }
    
    Visual BasicCopy Code
    <Table()> _
    Public Class Company
    
        <Column(IsPrimaryKey:=True)> _
        Public CompanyID As Integer
    
        <Association(Name:="Company_Order", Storage:="_Orders", OtherKey:="CompanyID")> _
        Public Property Orders As EntitySet(Of Order)
            Get
                Return Me._Orders
            End Get
            Set(value As EntitySet(Of Order))
                Me._Orders.Assign(value)
            End Set
        End Property
    
        Private _Orders As EntitySet(Of Order)
    End Class
    
    <Table()> _
    Public Class Order
    
        <Column(IsPrimaryKey:=True)> _
        Public OrderID As Integer
    
        <Column()> _
        Public CompanyID As Integer
    
        <Association(Name:="Company_Order", Storage:="_Company", ThisKey:="CompanyID")> _
        Public Property Company As Company
            Get
                Return Me._Company.Entity
            End Get
            Set(value As Company)
                Me._Company.Entity = value
            End Set
        End Property
    
        Private _Company As EntityRef(Of Company)
    End Class

    XML Mapping

    Association tag is used for association mapping.

    <Table>
      <Type Name="Company">
        <Column Member="CompanyID" IsPrimaryKey="true" />
        <Association Name="Company_Order" Member="Orders" Storage="_Orders" ThisKey="CompanyID" OtherKey="CompanyID" />
      </Type>
    </Table>
    <Table>
      <Type Name="Order">
        <Column Member="OrderID" IsPrimaryKey="true" />
        <Column Member="CompanyID" CanBeNull="false" />
        <Association Name="Company_Order" Member="Company" Storage="_Company" ThisKey="CompanyID" OtherKey="CompanyID" IsForeignKey="true" />
      </Type>
    </Table>

    Fluent Mapping

    FluentMappingBuilder class methods are used for mapping associations. See the Association Configuration Classes topic for more details.


    C#csharpCopy Code
    FluentMappingBuilder builder = new FluentMappingBuilder(null);
     
    builder.Entity<Order>()
        .Association()
            .Name(@"Company_Order")
            .ToOne(order => order.Company).ThisKey(order => new { order.CompanyID })
            .FromMany(company => company.Orders).OtherKey(company => new { company.CompanyID });
    
    Visual BasicCopy Code
    Dim builder As New FluentMappingBuilder(Nothing)
     
    builder.Entity(Of Order) _
        .Association() _
            .Name("Company_Order") _
            .ToOne(Function(order) order.Company).ThisKey(Function(order) New With {order.CompanyID}) _
            .FromMany(Function(company) company.Orders).OtherKey(Function(company) New With {company.CompanyID})