Archive for June, 2011

Entity Framework 4.1 has introduced the Code First Approach with it’s Fluent API and Annotations.In this series of posts we will focus on the Annotations part of it and how it can be used to build the domain model and map it to the database.

We will use the standard School data model to explain most of the stuff and take different examples in some of the special cases.The table diagram is shown below:

image

We will start with the very basics, i.e. I have POCO and I want to map it to a database table.I need to use the annotations TableAttribute and ColumnAttribute under System.ComponentModel.DataAnnotations as shown below

[Table("Course")] 
   public class Course 
   { 
       [Column("CourseID")] 
       public int CourseID { get; set; } 
       [Column("Title")] 
       public string Title { get; set; } 
       [Column("Credits")] 
       public int Credits { get; set; } 
       [Column("DepartmentID")] 
       public int DepartmentID { get; set; } 
              
   }

The TableAttribute also allows to specify the schema to which this table belongs to. Now the next thing is how to specify the primary key?. We have to use the KeyAttribute as shown below:

[Table("Course")] 
   public class Course 
   { 
       [Key] 
       [Column("CourseID")] 
       public int CourseID { get; set; } 
       [Column("Title")] 
       public string Title { get; set; } 
       [Column("Credits")] 
       public int Credits { get; set; } 
       [Column("DepartmentID")] 
       public int DepartmentID { get; set; } 
               
   }

In this table the Primary Key is an IDENTITY column automatically generated in the database. We need to use the DatabaseGeneratedAttribute to specify the same. There are three types of database generation strategy supported by the framework using the DatabaseGeneratedOption enumeration i.e

  • None
  • Identity
  • Computed

The completed class definition is shown below:

[Table("Course")] 
   public class Course 
   { 
       [Key] 
       [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)] 
       [Column("CourseID")] 
       public int CourseID { get; set; } 
       [Column("Title")] 
       public string Title { get; set; } 
       [Column("Credits")] 
       public int Credits { get; set; } 
       [Column("DepartmentID")] 
       public int DepartmentID { get; set; } 
              
   } 

Next thing how we can define Composite Keys? In the table CourseInstructor we have both CourseID and InstructorID as Primary Key. This can be easily defined using the KeyAttribute as shown below:

[Table("CourseInstructor")]
    public class CourseInstructor
    {
        [Key]
        [Column("PersonID")]
        public int PersonID { get; set; }
        [Column("CourseID")]
        [Key]
        public int CourseID { get; set; }
    }

While executing query on this object we get the following exception

Unable to determine composite primary key ordering for type ‘EFAnnoations.CourseInstructor’. Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys.

We will use the Order property of ColumnAttribute as shown below:

[Table("CourseInstructor")]
public class CourseInstructor
{
    [Key]
    [Column("PersonID",Order=0)]
    public int PersonID { get; set; }
    [Column("CourseID",Order=1)]
    [Key]
    public int CourseID { get; set; }
}

Next post onwards we will start exploring how we can establish the different types of relationship in our entity model.