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.

asp_dotnet_MVC_EntityFramework_CodeFirst_1

 

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

 

asp_dotnet_MVC_EntityFramework_CodeFirst_2

 

 

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

asp_dotnet_MVC_EntityFramework_CodeFirst_3.png

 

 

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.

asp_dotnet_MVC_EntityFramework_CodeFirst_4

 

asp_dotnet_MVC_EntityFramework_CodeFirst_4_1

 

 

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

 asp_dotnet_MVC_EntityFramework_CodeFirst_5

 

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:

asp_dotnet_MVC_EntityFramework_CodeFirst_6

 

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)

        {

            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        }

    }

 

asp_dotnet_MVC_EntityFramework_CodeFirst_7

 

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));

ctx.SaveChanges();

}

}

/asp_dotnet_MVC_EntityFramework_CodeFirst_8

 

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

 

  public class MvcApplication : System.Web.HttpApplication

    {

        protected void Application_Start()

        {

            AreaRegistration.RegisterAllAreas();

            RouteConfig.RegisterRoutes(RouteTable.Routes);

 

            Database.SetInitializer(new DBInitializer());

        }

    }

asp_dotnet_MVC_EntityFramework_CodeFirst_9.png

 

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

asp_dotnet_MVC_EntityFramework_CodeFirst_10

 

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 :

asp_dotnet_MVC_EntityFramework_CodeFirst_11.png

asp_dotnet_MVC_EntityFramework_CodeFirst_12.png

 

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

asp_dotnet_MVC_EntityFramework_CodeFirst_13.png

 

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

asp_dotnet_MVC_EntityFramework_CodeFirst_14.png

 

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 asp.net
Error 720 Resolution
Queries in LINQ to DataSet
BEWARE-XSS THIEVES ARE LOOKING FOR YOU
how to search date within range in c Sharp dot net and mssql

You may also like :





Responses