Tutorial 1: Defining Data Model Classes

Description
Table of Contents
Summary

The Data Model is defined by a set of POCO classes where each of these classes represents a data table in the relational database and each object instance represents a row within the database table.

These classes are referred to as “Entity Classes“.

Description

Entity classes are defined as partial classes where:

  1. A file contain the plain definition code of the entiity;
  2. A separate file can contain business logic and validation rules to be called within each property accessor and/or an entitiy is created, loaded, and validated prior to the process of submitting changes to the database.

(1) and (2) are merged together at compile time into a single file and within the data layer.

Business logic and validation rules can also be introduced as  partial methods definied on the DataContext class

Operations
Simple entitiy class
How to do it
public partial class Person
{
   private int _ID;
   private int _IDRole;
   private string _lastName;
   private string _firstName;

   public int ID
   {
      get { return _ID; }
      set { _ID = value; }
   }
}

   public int IDRole
   {
      get { return _IDRole; }
      set { _IDRole = value; }
   }

   public string LastName
   {
      get { return _lastName; }
      set { _lastName = value; }
   }

   public string FirstName
   {
      get { return _firstName; }
      set { _firstName = value; }
   }
}
Partial business methods on Entity class
How to do it
#region Extensibility Method Definitions

    // methods invoked on creation, load and submitting changes of an entity instance

    partial void OnLoaded();
    partial void OnValidate();
    partial void OnCreated();

// methods invoked on on property set accessors
of an entity instance

    partial void OnCustomerIDChanging(string value);
    partial void OnCustomerIDChanged();
    ...

#endregion
Partial business methods called for each entity within the DataContext class
How to do it
#region Extensibility Method Definitions

    // methods invoked on creation, update and deletion of an entitiy

    partial void OnCreated();
    partial void InsertCustomer(Customer instance);
    partial void UpdateCustomer(Customer instance);
    partial void DeleteCustomer(Customer instance);   

#endregion