Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.Net MVC CRUD (Insert, Update, Delete) Operations using Entity Framework

Nov 9, 2016
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.


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

This is how we can use Entity Framework in asp.net mvc to perform CRUD (insert, update, delete) operations based on our requirements.

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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

Alexander Grinvald said...

Thanks for detailed example!

Anonymous said...

wena hermana wm, te pasaste

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.