CRUD operation in ASP.NET MVC 4 using Entity Framework.

This article describes how to create a ASP.NET MVC Application Using Entity Framework or Basic CRUD operations in ASP.NET MVC using Entity Framework. To start we need some predefined basic knowledge about ASP.NET MVC. Summery of the article:

  • Database Design
  • Create a ASP.NET Web Application Project.
  • Adding a Database Model
  • Adding a Controller
  • Adding Views
  • Create Navigation
  • Run the Application

Database Design
Create a Database in the SQL Server named TestDB. Create a Students table in the database. The SQL scripts for Students table:

CREATE TABLE [dbo].[Students](
	[StudentID] [int] IDENTITY(1,1) NOT NULL,
	[StudentName] [nvarchar](150) NULL,
	[Address] [nvarchar](200) NULL,
	[Phone] [nvarchar](50) NULL,
 CONSTRAINT [PK_member] PRIMARY KEY CLUSTERED 
(
	[StudentID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, 
 ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Create a ASP.NET Web Application Project.
At first we need to Create a ASP.NET Web Application Project.If Microsoft Visual Studio installed, Start it and select New Project from the file menu.In the New Project dialog box:

  • From the Installed Templates pane Select Visual C#.
  • Select the template ASP.NET Web Application.
  • Set the project name to MyProject.
  • Set the disk location to something like C:\TestProject.
  • Click OK.

Adding a Database Model
We need to create a database model.Here we will include a ADO.NET Entity Data Model or EMD file. The steps to Create an EMD file:

EMDX File

Adding a Controller
We need to create a Controller for the Student. We can create a Controller by following steps:

  • Re-Build the project from the menu.
  • In the Solution Explorer, right-click the Controllers folder, and select Add and Controller
  • Set controller name to StudentController
  • Select template: Controller with read/write actions and views, using Entity Framework
  • Select model class: Students (MyProject.Models)
  • Select data context class: TestDBEntities(MyProject.Models)
  • Select views : ASPX(C#)
  • Click Add

Add Controller

If we see the “StudentController” we will found all the codes are automatically created for CRUD operations.

EF Controller

Visual Studio will create the following files:

  • A StudentController.cs file in the Controllers folder
  • A Student folder in the Views folder

Adding Views
After controller creation, the Visual Studio will automatically create the following files in the Student folder:

  • Create. aspx
  • Delete. aspx
  • Details. aspx
  • Edit. aspx
  • Index. aspx

Create Navigation
Create navigation in Site.Master for Student. We can do it by using the ActionLink method. Here is a sample code that navigates the “Student” controller and invokes the Index action.
<%: Html.ActionLink(“Student”, “Index”, “Student”) %>

Our Site.Master page will be like:

<ul id=”menu”>
<li><%: Html.ActionLink(“Home”, “Index”, “Home”) %></li>
<li><%: Html.ActionLink(“About”, “About”, “Home”) %></li>
<li><%: Html.ActionLink(“Contact”, “Contact”, “Home”)%></li>
<li><%: Html.ActionLink(“Student”, “Index”, “Student”) %></li>
</ul>

 

 

Run the Application
Select Debug, Start Debugging (or F5) from the Visual Web Developer menu. Our application will look like:

MVC4 Application

We can perform all the CRUD (create, read, update, delete) operations.

Create Operation

In this way we can select, insert, update, delete data in asp.net mvc.

Comments

  1. By Mular

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *