Tutorial 3: Configuring mapping between Database Table(s) and Data Model Class(es) using DataAnnotation (Attributes)

Description
Table of Contents
Summary

The mapping of each entity class of the Data Model to a database table is achived through the use of attributes to decorate the class as well as class properties and class fields.

Description

Mapping entity class and database table

The attribute used is the TableAttribute and is optional. By default the name of the class is associated to the database table with the same name.

Mapping fields and properties to database table columns

Each property elegible to be used with Lnq must be mapped to a column through the ColumnAttribute and its properties are defined in the table below:

PROPERTY DESCRIPTION
AutoSync Specifies if the column is automatically synchronized from the value generated by the database on insert or update commands. Valid values for this tag are Default, Always, Never, OnInsert, and OnUpdate.
CanBeNull A Boolean value that indicates if the column can contain null values (true) or not (false).
DbType Specifies the column’s data type in the database. Ifyou omit this property, LINQ will infer the type from the class member. This property is mandatory only if you want to use the CreateDatabase method to create a new database instance.
Expression Defines the column as a computed column. Using this attribute you can define the formula used to compute the result.
IsDbGenerated Identifies a column whose value is generated by the database. Usually used in conjunction with primary key columns defined with the IDENTITY property.
IsDiscriminator Indicates that the member holds the discriminator value for an inheritance hierarchy.
IsPrimaryKey Specifies that a column is part of a table’s primary (or unique) key. LINQ currently works only with tables that have primary (or unique) keys.
IsVersion Indicates the member is a database timestamp or version number.
Name Specifies the column’s name in the database. Defaults to the member name.
Storage Specifies the name of the private field underlying a property. LINQ will bypass the property’s get and set accessors and use the field instead.
UpdateCheck Specifies how LINQ detects optimistic concurrency conflicts. The possible values are Always, Never, and WhenChanged. If no member is marked with IsVersion=true, all members participate in detection unless explicitly specified otherwise.
Operations
Mapping class and properties
How to do it
[Table(Name="Person")]
public class Person
{
   private int _ID;
   private int _IDRole;
   private string _lastName;
   private string _firstName;

   [Column(Name="ID", Storage="_ID",
       DbType="int NOT NULL IDENTITY",
       IsPrimaryKey=true, IsDbGenerated=true)]
   public int ID
   {
      get { return _ID; }
      set { _ID = value; }
   }

   [Column(Name="IDRole", Storage="_IDRole",
      DbType="int NOT NULL")]
   public int IDRole
   {
      get { return _IDRole; }
      set { _IDRole = value; }
   }

   [Column(Name="LastName", Storage="_lastName",
     DbType="nvarchar NOT NULL")]
   public string LastName
   {
      get { return _lastName; }
      set { _lastName = value; }
   }

   [Column(Name="FirstName", Storage="_firstName",
     DbType="nvarchar NOT NULL")]
   public string FirstName
   {
      get { return _firstName; }
      set { _firstName = value; }
   }
}