Basic steps to implement ASP dotnet MVC using Entity Framework Code First approach

On November 24

Basic steps to implement ASP dotnet MVC using Entity Framework Code First approach

In this tutorial we will see very basic steps to implement ASP dotnet MVC using Entity Framework Code First approach. ASP Dotnet MVC is a framework provided by Microsoft to implement application using Model View Controller Design pattern. Further to ease interaction with to MVC Entity framework is been introduced. Today we will see the code first approach for which following are the steps:


Step 1: Open Visual Studio and click on the New Project.



Step 2 : Select web - > ASP .Net Web Application. Name It as TestApp and click on ok.





Step 3: Then select empty template and in Add folder and reference tick on MVC and click on ok:




Step 4: Now open package manager console and in that type the following command and enter       “Install-Package EntityFramework”. This will install the latest entity framework reference in your project.






Step 5: Now add class employee in your Model folder:



Step6: Add the following attributes to the employee model: 

public class employee


public int Id { get; set; }

public string Name { get; set; }

public DateTime DoB { get; set; }

public Gender Gender { get; set; }

public int Flag { get; set; }


public enum Gender


        Male, Female, Other




Step 7: Now add folder to the solution Datacontext:



Step 8: Now in Datacontext folder add class as Datacontext.cs and inherit it from DBContext. And add the following code:

public class Datacontext: DbContext


        public Datacontext() : base("EmployeeDatatbase ")

        { }

        public DbSet<employee> Employees { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)








Step 9: Now add class DBInitializer to DataContext  folder. And add the following code to it :

publicclassDBInitializer : DropCreateDatabaseIfModelChanges<Datacontext>


protectedoverridevoid Seed(Datacontextctx)


var employees = newList<employee>()


newemployee() {Name="Employee1",DoB=DateTime.Parse("1990-11-21"),Gender=Gender.Male,Flag=1



employees.ForEach(x =>ctx.Employees.Add(x));






Step 10: Now add the following code to your Global.asax file :


  public class MvcApplication : System.Web.HttpApplication


        protected void Application_Start()





            Database.SetInitializer(new DBInitializer());





Step 11: Give connection string in your web config as follows:



Step 12: Now build the solution and in controller folder add controller with name as employeesControllerwith select as MVC 5 Controller with views using Entity Framework :




Step 13: Now change controller to employees in route.config in App_start folder:



Step 14: Run the project and you will see that the database is been created automatically and the table are been initialized.



You may also like :

Basic steps to implement ASP dotnet MVC using Entity Framework Code First approach
SQL Query to Release lock on database
How to change URL withought page postback
Csharp Code to make call By connecting you phone to PC
BE Electronics ETC IT Computer Project Titles - Group 1
SQL Query to Split string by specific seperator
Simple Page Method in
Error 720 Resolution
Queries in LINQ to DataSet
how to search date within range in c Sharp dot net and mssql

You may also like :