Introduction:
Here I will explain how to perform crud operations in asp.net mvc using entity framework with example or asp.net mvc crud (insert, update, delete) operations with examples or crud operations using entity framework in asp.net mvc with example or insert, update and delete operations in asp.net mvc with entity framework example. In asp.net mvc we can easily perform crud (insert, update, delete) operations using entity framework without writing single of code.
Description:
In previous articles I explained asp.net mvc bind dropdownlist with example, asp.net mvc get dropdownlist selected value, asp.net mvc redirect to another view or controller action method, asp.net mvc show alert message with example, asp.net mvc get data from database with example, difference between asp.net web api and web service with example, web api in asp.net and consume web api in asp.net with example and many articles relating to asp.net mvc, asp.net, c#,vb.net. Now I will explain how to perform crud (insert, update, delete) operations in asp.net mvc with example.
In previous articles I explained asp.net mvc bind dropdownlist with example, asp.net mvc get dropdownlist selected value, asp.net mvc redirect to another view or controller action method, asp.net mvc show alert message with example, asp.net mvc get data from database with example, difference between asp.net web api and web service with example, web api in asp.net and consume web api in asp.net with example and many articles relating to asp.net mvc, asp.net, c#,vb.net. Now I will explain how to perform crud (insert, update, delete) operations in asp.net mvc with example.
By
using entity framework we can perform CRUD (insert, update, delete) operations
on database tables in asp.net
mvc easily
without writing any cod for that first design userdetails table in database and insert some data like as shown
below.
Column Name
|
Data Type
|
Allow Nulls
|
userid
|
Int(IDENTITY=TRUE)
|
NO
|
username
|
varchar(50)
|
Yes
|
education
|
Varchar(50)
|
Yes
|
location
|
Varchar(50)
|
Yes
|
Or
use following query to create userdetails
table in database and insert some data to bind values to dropdownlist.
create table userdetails(
userid int primary key identity,
username varchar(50),
education varchar(50),
location varchar(50)
)
INSERT INTO userdetails(username,education,location)
values('Suresh Dasari','B.Tech','Chennai'),
('Rohini Alavala','Msc','Guntur'),
('Praveen Kumar','B.Tech','Bangalore'),
('Madhav Sai','MBA','Nagpur')
|
Once we create userdetails table now create asp.net
mvc application for that Open visual studio à Go to File à Select New à Project like as
shown below
Once we select Project new popup will open in
that select Asp.Net Web Application and give name to application and
click OK like as shown below
Once click OK new popup will open in that
select Empty template and select
folders and core reference as MVC
and click OK like as shown below
Once we finished creating application our
project structure will be like as shown below
Now we will install Entity Framework component
in our project for that right click on our project select “Manage Nuget Packages” like as shown below
Now search for “Entity Framework” and install it like as shown below
Once “Entity
Framework” installation completed means it will show Green tick icon like
as shown below.
Now we will add new ADO.NET Entity Data Model to our application for that right click
on Models folder à select Add à select ADO.NET Entity
Data Model like as shown below
Once we click on ADO.NET Entity Data Model new
popup will open in that enter name of model like as shown below.
Once we enter name for model click OK button
new popup will open in that select “EF
Designer from Database” and click Next button like as shown below.
In next window, we need to configure required
database connection for that click on “New
Connection” like as shown below.
Once we click on “New Connection” new popup will open in that configure required
database like as shown below.
Once server and database configuration
finished click on OK button then it
will format database connection like as shown below.
In case if you need different name for entity
model then you can give your custom name and click Next to select required tables to use it in our application.
Once we select required tables click Finish button then it will create
required dbcontext files and model files like as shown below.
Now
we will add new controller to get data from database for that right click on
Controller folder à select Add à Controller like as shown
below
Once we click on
Controller new popup will open in that select MVC 5 Controller with views, using Entity Framework and click Add like as shown below.
Once click on Add new
window will open in that select Model
class, Data context class and
give name of controller and click Add like as shown below
Once we add controller
automatically views will create like as shown below we don’t need to write
anything everything created by Entity
Framework.
Now open newly created
controller that will contain code like as shown below
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using efmvcexamples.Models;
namespace efmvcexamples.Controllers
{
public class userdetailsController : Controller
{
private MySamplesDBEntities db = new MySamplesDBEntities();
// GET: userdetails
public ActionResult Index()
{
return View(db.userdetails.ToList());
}
// GET:
userdetails/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
userdetail userdetail = db.userdetails.Find(id);
if (userdetail == null)
{
return HttpNotFound();
}
return View(userdetail);
}
// GET:
userdetails/Create
public ActionResult Create()
{
return View();
}
// POST:
userdetails/Create
// To protect from
overposting attacks, please enable the specific properties you want to bind
to, for
// more details see
http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "userid,username,education,location")] userdetail userdetail)
{
if (ModelState.IsValid)
{
db.userdetails.Add(userdetail);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(userdetail);
}
// GET:
userdetails/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
userdetail userdetail = db.userdetails.Find(id);
if (userdetail == null)
{
return HttpNotFound();
}
return View(userdetail);
}
// POST:
userdetails/Edit/5
// To protect from
overposting attacks, please enable the specific properties you want to bind
to, for
// more details see
http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include ="userid,username,education,location")] userdetail userdetail)
{
if (ModelState.IsValid)
{
db.Entry(userdetail).State
= EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(userdetail);
}
// GET:
userdetails/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
userdetail userdetail = db.userdetails.Find(id);
if (userdetail == null)
{
return HttpNotFound();
}
return View(userdetail);
}
// POST:
userdetails/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
userdetail userdetail = db.userdetails.Find(id);
db.userdetails.Remove(userdetail);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
|
That’s it we done don’t
need to write any code automatically everything configured by Entity Framework.
Now we will run and see
the application result. (url always in the format of http://localhost:portnumber/controller name/action method name) and
check the output that would be like as shown below
To Create new user
details our page will be like as shown below
To see particular user details our page will be like as shown below
To Edit user details our
page will be like as shown below
To Delete user details
our page will be like as shown below
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
11 comments :
Thanks for detailed example!
wena hermana wm, te pasaste
it's very helpful to understand CrudOpeariton thanks....!
i want to download to code can you mail me vihari17@gmail.com
can you help i want to do crud operations for GRID.MVC help me
hi
could you able to provide view code for this example.
thank you sir....
nice tutorial
nice
can you help i want to download to code can you mail me amsalu743@gmail.com
After I sucessfully installed of entity framework,
I could find add ado.net entity data model.
Pls help me to solve
thanks for this article
Note: Only a member of this blog may post a comment.