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.
Requirement:
I am using Visual studio 2012 to develop this project.
Description:
In the previous article I have explained How to integrate Captcha in asp.net,What is Scaffolding in Asp.net MVC?, How to create Contact Us page in Asp.net and What is Asp.net MVC? Its advantages and disadvantges.
To add a MVC project go to File>>New>>Project as shown in attached snapshot.
(Click to enlarge)
After that go to Web>>Asp.net
MVC4 Web Application see attached snapshot.
(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.
(Click to enlarge)
Add a Model Class:
To add a Model right click on Models>>Add>>Class as show in attached snapshot.
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.
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:
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:
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:
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.
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.
No comments:
Post a Comment