Tuesday, July 26, 2011

[Tutorial] Code First with Entity Framework 4.1
Part2: Data Annotations & Code First Fluent API

The new version of Entity Framework supports the usage of Data Annotations. Data Annotations are used for validation purposes and also for mapping definitions between code and columns in the database.

All annotations are implemented as attributes which can be found in the System.ComponentModel.DataAnnotations  namespace (same attributes that are used for ASP.NET MVC validation purposes).

Common attributes:

  • [Key] – Column(s) that define the Primary Key in the database table
  • [StringLength] – Minimal and maximal string length (only used for validation purposes not as database constraints)
  • [MaxLength] – Can be used instead of [StringLength] to just define the maximal string length of a column
  • [ConcurrencyCheck] – Flag that indicates that concurrency checks are activated for the column 
  • [Required] – Flag that indicates that a value is required for the column (column is flagged as not null)
  • [Timestamp] – Flag on time stamp columns which is used for concurrency checks
  • [Column] – Specify the name of a column (default property name), can also be used to define the column position
  • [Table] – Specify the name of a table (default class name)
  • [DatabaseGenerated] – Flag that indicates that the value is filled by the database (possible values are Computed, Identity, None)
  • [NotMapped] – Is used to define a property in the class that does not get generated in the database.
  • [ForeignKey] and [InverseProperty] – Column(s) that are defined as Foreign Keys

Following an example for the usage of Data Annotations that was applied to our company domain. The Manager class does not currently contain any properties that could automatically be identified as primary key.

By adding the [Key] attribute to the ManagerCode property you define that this property should act as primary key. You furthermore add a concurrency check as well as a minimum and maximum length setting for the Name property.

image

In the following example we add a required constraint to the Department class by applying the [Required] Data Annotation to the Name property.

image

Data Annotations are definitely easy to use but it  is preferable to use a programmatic approach that provides much more flexibility.  This approach is based on the Code First Fluent API.

You have to implement the OnModelCreating method within the derived CompanyContext class. Then you define your constraints applicable to your domain within this method programmatically.

image

When using this approach you may even describe relations between tables and columns. The following code example defines that the Manager entity contains a Department entity. The key that relates both is the DepartmentId.  If the Manager entity is deleted then the corresponding Department must also be deleted  (CascadeOnDelete).

image


Share/Save/Bookmark

2 comments:

Anonymous said...

I was curious if you ever thought of changing
the layout of your blog? Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content so people could
connect with it better. Youve got an awful lot of text for only having
1 or two pictures. Maybe you could space it out
better?

My blog - company intends

Jason De Oliveira said...

I will definitely think about your request and see when I have some time to decide on a new layout. Thank you for your remark !