ASP.NET MVC 5 – CRUD operations with Entity Framework 6 on Visual Studio 2013

ASP.NET MVC 5 – CRUD operations with Entity Framework 6 on Visual Studio 2013

This demo focuses on using ASP.NET Scaffolding to generate the 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

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:    
C#
public class Movie 
{ 
    public int ID { getset; } 
    public string Title { getset; } 
    public DateTime ReleaseDate { getset; } 
    public string Genre { getset; } 
    public decimal Price { getset; } 
} 
 
public class MovieDBContext : DbContext 
{ 
    public DbSet<Movie> Movies { getset; } 
} 
  • Put the class inside the Models folders, just to organize your code.  

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

  •  Choose option MVC5 Controller with views using Entity Framework  

  • 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

C#
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  

  • Run the application  

MVC5 Resources

Some good online resources about MVC5:

Code Samples

All of this sample can be found and downloaded in Microsoft Code Gallery:
Sort by: Published Date | Most Recent | Most Useful
Comments
Page 1 of 1 (3 items)