Here, we are going to learn about the Code First approach in MVC. Basically, I am used to the Database-First approach. It is obvious that both the Code-First approach and DB First approach are useful in their own context but I would like to summarize the basic usage of these as per my working experience.
When to use DB-First Approach?
- When you have to work in a project where DB is already created.
- You don’t have to alter DB columns, stored procedures, and views.
- When your project is small and doesn’t have to be very modular.
- When you don’t want to get a headache writing simple classes because DB-First approach in Entity will write all the code for class.
When to use Code- First?
- When you have to make your application from scratch.
- When you don’t have a readymade database.
- You have to play with database frequently, like creating and deleting tables, views, and stored procedure.
- When you have too many tables, views, and stored procedures in database.
What we will learn?
- Creating a blank database.
- Creating a MVC Project (UI)
- Creating a Class Library Project (DAL)
- Code First Approach implementation.
- Migration in MVC
- Creating CRUD Operation.
Creating Blank Database
Step 1
Open SQL Server.
Step 2
Connect to Database Server.

Step 3

You will be able to see your newly created database on Server.
Creating a MVC Project
Step 1
Open Visual Studio.
Step 2
Go to File => New Project.
- Select Visual C# => Web => ASP.NET Web Application.
- Name your Solution (eg. EasyCRM) and Project (EasyCRM.UI) and then click OK.
- Select MVC.
- Change Authentication to "Individual User Accounts".
Creating a class Library Project (DAL)
Step 1
Right click on Solution Explorer to add new project.

Step 2
Select Visual C# => Windows => Class Library.

Adding Entity Framework to DAL Project.
Step 1
Right click on DAL Project and select "Manage NuGet Packages".

Step 2
Browse Entity Framework and Install.

Code First Approach
Now, add 3 folders to EasyCRM.DAL Project named as
- DataConnection
- Entity
- Identity
Creating Entity for Customer
- Add a new folder inside Entity, named as Customer.
- Add New Class called Customer_PersonalDetail.cs.
- Add the following code inside the class.
- public string CP { get; set;}
- [Required(ErrorMessage = "City required")]
- public string City {get;set;}
- [Required(ErrorMessage = "Contact person required")]
- [DisplayName("Contact person")]
- public string ContactPerson {get;set;}
- [Required(ErrorMessage = "Telephone required")]
- [DisplayName("Telephone")]
- [Phone]
- public string Phone {get;set;}
- [Required(ErrorMessage = "Email required")]
- [RegularExpression(@ "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Wrong email format")]
- [EmailAddress]
- public string Email {get;set;}
- public string Notes {get;set;}
- Add new class called "Users" under Entity folder.
- The new class file should look like it -
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.AspNet.Identity.EntityFramework;
- using System.ComponentModel.DataAnnotations;
- namespace EasyCRM.DAL.Entity {
- public class Users IdentityUser {
- [Required]
- public string Name {get;set;}
- [Required]
- public string Address get;set;}
- [Required]
- public string Gender {get; set; }
- }
- }
















kaarthikeyan tkPosted May 15, 2021, 7:54 AM
Can you plz add connection string
GoGi FeboPosted Mar 11, 2021, 10:23 PM
Problem with context....... a waste of time.....
Ijaz KhanPosted Apr 10, 2020, 5:00 AM
The concept is good but their is a problem in Db Context class in which the code are mixed.
Abu SafyanPosted Oct 24, 2019, 10:20 AM
Very Bad Code
Pravin LalgePosted Jan 14, 2019, 12:42 AM
Very bad code
sne sPosted Jul 19, 2018, 7:44 PM
Where to download the source code from ??
Zvonko JocicPosted Jul 16, 2018, 4:57 PM
Code is bad. User Entity file has errors and context file is not correct, please correct so we can proceed with tutorial. Thanks
Upendra Pratap ShahiPosted Sep 5, 2017, 7:45 AM
PublicclassDataContext IdentityDbContext <Users> { publicDataContext() base("DefaultConnection") {} publicDbSet <Customer_PersonalDetail> Customer_PersonalDetail { get;set;} } line give error in VS2015
Bikesh SrivastavaPosted Jan 25, 2017, 8:43 AM
Very nice article ,well explained.
Naveen BishtPosted Jan 24, 2017, 3:08 AM
Hey Nishan thanks it is very easy to understand, i have one question here if i want run SQL query in code first approch. is it possible? if yes then how. for eg. in DB first we directly do context.Database.SqlQuery<myEntityType>( "mySpName @param1, @param2, @param3", new SqlParameter("param1", param1), new SqlParameter("param2", param2), new SqlParameter("param3", param3) );