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:

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

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