Entity Framework 4.1 Annotations–Part 1

Posted: June 5, 2011 in .NET, ADO.NET Entity Framework
Tags: , ,

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.

About these ads
Comments
  1. Entity Framework 4.1 Annotations–Part 1 « Sankarsan’s Journal…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  2. Sumit Bisht says:

    Is reverse engineering of POCOs from existing database possible with the annotations approach (like hibernate reveng ) ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s