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
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:
- In the Solution Explorer, right-click the Models folder, and select Add New Item.
- Select the template Data and select ADO.NET Entity Data Model.
- Set the name as StudentModel.edmx
- Click Add.
- Follow every steps to configur or create the ADO.NET Entity Data Model or EMD 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
If we see the “StudentController” we will found all the codes are automatically created for CRUD operations.
Visual Studio will create the following files:
- A StudentController.cs file in the Controllers folder
- A Student folder in the Views folder
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 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:
<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>
Run the Application
Select Debug, Start Debugging (or F5) from the Visual Web Developer menu. Our application will look like:
We can perform all the CRUD (create, read, update, delete) operations.
In this way we can select, insert, update, delete data in asp.net mvc.