Click here to Skip to main content
13,092,725 members (84,874 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 7 Jun 2012

Starting Entity Framework Codefirst Approach: Part 2

, 7 Jun 2012
Rate this:
Please Sign up or sign in to vote.
This is the second part of my Entity Framework Code First Tutorial for beginners.

This is the second part of my Entity Framework Code First Tutorial for beginners. If you want to start fresh, visit the first part here.

If you tried the first part, while creating the database using code first approach, you might have noticed that our columns of string type are created as nullable columns of type nvarachar and has the MAX size.

Obviously, it is not a good idea to create table columns like this. We need to be more specific about what we are going to build and work with. So if you want to specify the size /nullabilty, we can do that as well in few different ways.

1) Data Annotation

Data Annotations are attributes we can apply to the class members. By decorating a class member / Property with data annotation, we can achieve the following things:

  1. Impose some validations on the entity
  2. Specify how the data is going to look like in the UI when displayed
  3. Specify some relationships between entities

Let us simply decorate our customer class attributes with some data annotations.

So what are we getting out of this? Run the project and see what change it brings to our database.

The Required attribute made our column to be a not null column and the Max Length Attribute value becomes the size of the column.

2) Fluent API

Fluent API is another way to configure the Entity Properties or the relationship among the entities. What we did with data annotations in the above example can be achieved with Fluent API as well. Why do we need to use Fluent API over Data Annotations or vice versa? Well, I guess it is more of a personal preference. Some people like to keep their classes clean. They just want to have the property definition only in the classes and don't want to define these attributes in that class. Fluent API helps those people to keep these configurations in a different place.

Entity framework will generate the database by looking at the classes we have (not all the classes, but those classes which we are using inside our DBContext class to create properties of type DBSet.) created. So there is a way we can override some of the configuration. There is an overridable method called OnModelCreating inside DBContext. So we can override that in our DbContext class to specify some of the configuration we want to apply when entity framework creates tables for us from the classes.

I am going to create another entity class to show this. Let us create a class called Address under our Model folder. Add Properties like below to the Address entity.

Wait: Include a reference to System.ComponentModel.DataAnnotations namespace in this class with a using System.ComponentModel.DataAnnotations statement. We are going to use the data annotations present in this namespace in our class.

Note that we did not add any data annotations to the properties of the Address class. So when we run the project, EF will create nullable nvarchar columns with MAX length for all those string properties.

Now to override the default configuration, go to our DBContext class (SampleContext.cs) and add the below method.

Tip: You may simply type “override” inside the class and VisualStudio intellisense will show you all virtual methods available to be overridden. Just select OnModelCreating from that.

We can now use the DBModelBuilder object which is an argument of this method, to configure our entity / entity properties. Add some configurations in this method like below.

This code is pretty much self explanatory. It is saying that The Address Line1 Property of Address Entity should be Required field (not null) and should have a Max length of 100 (the column size). Run the project and see what output we are getting.

You can see that we are chaining methods here. That is something beautiful about this approach.

What if I have so many entities? I will have so much of code in this method and that is worse! We don't want to do that. Wait! There is a much better approach. Little more refactoring! We can move the configurations for an entity in a different class and just specify it in our OnModelCreating method. To show that, let me add another class to our model folder called PhoneNumbers.

Now to define the configuration, we will create another class called PhoneNumberConfiguration. We will inherit this class from EntityTypeConfiguration and specify the type while inheriting. In the Constructor, we can define our configuration like this:

Now we will go back to our OnModelBuilder method and add the PhoneNumber configuration class instance to the Configurations property of modelBuider.

And you will see the results as:

TIP: You need to include System.Data.Entity.ModelConfiguration namespace in your PhoneNumber configuration class as EntityTypeConfiguration class belongs to that.

You can download the source code of this example here for references. Leave a comment if you download the source code from this page / this article helped you.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
Shyju is a curious developer who loves to build highly scalabale and robust software on top of Microsoft development stack. He is a strong believer in test driven development and occasionally writes Tips & Tricks on ASP.NET MVC, Entity Framework, jQuery in his technical blog

Twitter : kshyju
Blog : TechiesWeb

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
rajesh224597719-Feb-13 19:57
memberrajesh224597719-Feb-13 19:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 7 Jun 2012
Article Copyright 2012 by kshyju
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid