LinqConnect Documentation
In This Topic
    Creating Entity Classes
    In This Topic

    We will start with a simple class Company and associate it with the Company table in the CRM_DEMO sample database. To do this, we need only apply a Table attribute to the top of the class declaration.

    [Table(Name="Company")]
    public class Company
    {
       ...
    }
    <Table(Name:="Company")> _
    Public Class Company
       ...
    End Class
    

    The Table attribute has a Name property that you can use to specify the exact name of the database table. If no Name property is supplied, LinqConnect assumes the database table has the same name as the class. Only instances of classes declared as tables will be stored in the database. Instances of these types of classes are known as entities. The classes themselves are known as entity classes.

    Also you need to denote each field or property you intend to associate with a database column. For this, LinqConnect defines the Column attribute.

    [Table(Name="Company")]
    public class Company
    {
       [Column(IsPrimaryKey=true)]
       public int CompanyID;
       [Column]
       public string CompanyName;
       [Column]
       public string PrimaryContact;
       [Column]
       public string City;
       [Column]
       public string Country;
       [Column]
       public string PrimaryContact;
       [Column]
       public string Web;
       [Column]
       public string Email;
       [Column]
       public string AddressTitle;
       [Column]
       public string Address;
       [Column]
       public string Region;
       [Column]
       public string PostalCode;
       [Column]
       public string Phone;
       [Column]
       public string Fax;
    }
    <Table(Name:="Company")> _
    Public Class Company
       <Column(IsPrimaryKey:=true)> _
       Public CompanyID As Integer
       <Column> _
       Public CompanyName As String
       <Column> _
       Public PrimaryContact As String
       <Column> _
       Public City As String
       <Column> _
       Public Country As String
       <Column> _
       Public PrimaryContact As String
        <Column> _
       Public Web As String
        <Column> _
       Public Email As String
        <Column> _
       Public AddressTitle As String
        <Column> _
       Public Region As String
        <Column> _
       Public PostalCode As String
        <Column> _
       Public Phone As String
        <Column> _
       Public Fax As String
    End Class
    

    The Column attribute has a variety of properties you can use to customize the exact mapping between your fields and the database columns. IsPrimaryKey is one of the most important properties. It tells LinqConnect that the database column is a part of the table primary key.

    Again, you only need to supply information in the Column attribute if it differs from what can be deduced from your field or property declaration. In this example, you have to tell LinqConnect that the CompanyID field is a part of the primary key in the table, yet you don't have to specify the exact name or type.

    Only fields and properties declared as columns will be persisted to or retrieved from the database. Others will be considered as transient parts of your objects.

    Let's create an Order class in the same way:

    [Table(Name="Orders")]
    public class Order
    {
       [Column(IsPrimaryKey = true)]
       public int OrderID;
       [Column]
       public int CompanyID;
       [Column]
       public int ContactID;
       [Column]
       public DateTime OrderDate;
       [Column]
       public double Freight;
       [Column]
       public DateTime ShipDate;
       [Column]
       public int ShipCompanyID;
       [Column]
       public double Discount;
    }
    <Table(Name:="Orders")> _
    Public Class Order
    
       <Column(IsPrimaryKey:=true)> _
       Public OrderID As Integer
       <Column> _
       Public CompanyID As Integer
       <Column> _
       Public ContactID As Integer
       <Column> _
       Public OrderDate As DateTime
       <Column> _
       Public Freight As Double
       <Column> _
       Public ShipDate As DateTime
       <Column> _
       Public ShipCompanyID As Integer
       <Column> _
       Public Discount As Double
    End Class
    

    This topic is continued in: Defining Relationships.