Create, Read, Update and Delete in Asp.net MVC 4 using Code first approach | ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial

Create, Read, Update and Delete in Asp.net MVC 4 using Code first approach

Introduction: In this article I will explain how we can Create, Read, Update and Delete in Asp.net with MVC 4 Razor view Engine using Entity framework with Code first approach.
MVC 4 Razor view Engine

Requirement:
I am using Visual studio 2012 to develop this project.

Description:

Add a MVC Project:

To add a MVC project go to File>>New>>Project as shown in attached snapshot.

MVC 4 Razor view Engine
                                                                                             (Click to enlarge)
After that go to Web>>Asp.net MVC4 Web Application see attached snapshot. 
MVC 4 Razor view Engine
                                                                                                   (Click to enlarge)
Here my project name is MVCAPPLICATION
Now a pop up window will be open select Internet Application>> Razor from view engine and hit Ok button as mention in snapshot.
MVC 4 Razor view Engine
                                                                                               (Click to enlarge)

Add a Model Class:

To add a Model right click on Models>>Add>>Class as show in attached snapshot.

MVC 4 Razor view Engine
                                                                                              (Click to enlarge)
Here I add a class and name it Student_Detail.cs. Add the code to class:
public class Student_Detail
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Subject { get; set; }
        public int Marks { get; set; }
    } 
Add another class ProjectContext.cs for Dbcontext and write the below given code:
using System.Data.Entity;

public class ProjectContext : DbContext
    {
        public ProjectContext() : base("name=MvcApplicationConnectionString")
    {
    }
        public DbSet<Student_Detail> Student_Detail { get; set; }
    }

Connect to Database:

Here I create a database named MVC_Database and add a connection as mention in snapshot.

MVC 4 Razor view Engine
                                                                                           (Click to enlarge)
After add a ConnectionString in web.config file :
  <add name="MvcApplicationConnectionString" connectionString="Data Source=dell-pc;Initial Catalog=MVC_Database;Integrated Security=True" providerName="System.Data.SqlClient"/>

Add EntityFramework:

To add/Install EntityFramework go to Tools>>Library Package Manager >> Package Manager Console as shown in snapshot:

MVC 4 Razor view Engine
                                                                                                    (Click to enlarge)
Package Manager Console will be open and type the given command:
Install-Package EntityFramework -Version 5.0.0
And hit enter button. See attached snapshot:

MVC 4 Razor view Engine
                                                                                               (Click to enlarge)
Build the project.
Note : Entity Framework-version 5.0 work with MVC 4.

Add a Controller:

To add a Controller right click on Controllers>>Add>>Controller. See attached snapshot:

MVC 4 Razor view Engine
                                                                                           (Click to enlarge)

Controller Name: StudentController 
Template: MVC controller with read/write actions and views, using Entity Framework
 Model Class : Student_Detail
Data context class : ProjectContext
As highlighted in snapshot. 
MVC 4 Razor view Engine
                                                                                            (Click to enlarge)

After that click on Add button, Visual Studio will create the following file/folder in project:
  •  StudentController.cs file in Controllers folder of project with Create, Read, Update and Delete functions.
  •  Student folder in Views containing Create.cshtml, Delete.cshtml, Details.cshtml, Edit.cshtml and Index.cshtml.
You have created the MVC application with Create, Read, Update and Delete functionality using Scaffolding. Now run the application and enter the URL in browser:

you will also see a table in Database with name Student_Detail has been created.

Is this article helpful for you?

If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

Post a Comment

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget