ASP.NET MVC 5 – CRUD operations with Entity Framework 6 on Visual Studio 2013
This demo focuses on using ASP.NET Scaffolding to generate controllers and views.
Introduction
Using MVC, Entity Framework, and ASP.NET Scaffolding, you can create a web application that provides an interface to an existing database table. This demo shows you how to automatically generate code that enables users to display, edit, create, and delete data that resides on the database. The generated code corresponds to the columns in the database table.
STEP 1 - Create new project on Visual Studio 2013
- Open Visual Studio
- Create new ASP.NET Web Application Project
- Give a name to the project
- In this case I call it "MVC5" as I show in the follow image
http://code.msdn.microsoft.com/site/view/file/106306/1/MVC51.jpg
STEP 2 - Create class Movies
- Add new class to the project
- Note: This class represents a table, and the properties of the columns of that table
- Give a name to that class
- In my sample I call it "MoviesModel"
- On the class create the following code:
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
public class MovieDBContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
- Put the class inside the Models folders, just to organize your code.
http://code.msdn.microsoft.com/site/view/file/106307/1/MVC52.jpg
STEP 3 - Create controller with views using Entity Framework
- Add new Scaffolded item to the project
- This is a new item which exists on MVC5
http://code.msdn.microsoft.com/site/view/file/106308/1/MVC53.jpg
- Choose option MVC5 Controller with views using Entity Framework
http://code.msdn.microsoft.com/site/view/file/106309/1/MVC54.jpg
- Click Add
- Note: If you receive an error, it may be because you did not build the project in the previous section. If so, try building the project, and then add the scaffolded item again.
After the code generation process is complete, you will see a new controller and views in your project.
STEP 4 - Controller and Views Generation
a. The Controller was automatically created with CRUD operations
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MVC5.Models;
namespace MVC5
{
public class MoviesController : Controller
{
private MovieDBContext db = new MovieDBContext();
// GET: /Movies/
public ActionResult Index()
{
return View(db.Movies.ToList());
}
// GET: /Movies/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
// GET: /Movies/Create
public ActionResult Create()
{
return View();
}
// POST: /Movies/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="ID,Title,ReleaseDate,Genre,Price")] Movie movie)
{
if (ModelState.IsValid)
{
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
// GET: /Movies/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
// POST: /Movies/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="ID,Title,ReleaseDate,Genre,Price")] Movie movie)
{
if (ModelState.IsValid)
{
db.Entry(movie).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
// GET: /Movies/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
// POST: /Movies/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Movie movie = db.Movies.Find(id);
db.Movies.Remove(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
b. The Views were automatically created
http://code.msdn.microsoft.com/site/view/file/106310/1/MVC55.jpg
- Run the application
http://code.msdn.microsoft.com/site/view/file/106311/1/MVC56.jpg
http://code.msdn.microsoft.com/site/view/file/106312/1/MVC57.jpg
MVC5 Resources
Some good online resources about MVC5:
- ASP.NET MVC5 : The official Microsoft .NET WebSite - http://www.asp.net/mvc/tutorials/mvc-5
- My personal blog: http://joaoeduardosousa.wordpress.com/
- ASP.NET Portal
Code Samples
All of this sample can be found and downloaded in Microsoft Code Gallery: