This is the second part of my Entity Framework Code First tutorial for beginners. If you want to start fresh.
Read the first part here.
If you tried the first part, while creating the database using Code First approach, you might have noticed that columns of string type are created as nullable columns of type
nvarachar and have 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 built 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
- Impose some validations on the entity
- Specify how the data is going to look like in the UI when displayed
- 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.
Required attribute made our column to be a not null column and the Max Length Attribute value become 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 to define these attributes in that class. Fluent API helps those people to keep these configuration 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 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
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 the
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 Visual Studio 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
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
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
And you will see the results as:
Tip: You need to include the
System.Data.Entity.ModelConfiguration namespace in your
PhoneNumber configuration class as the
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.