Share via


ASP.NET MVC 5 – Entity Framework 6, CRUD Operations on Visual Studio 2015

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.

This demo focuses on using ASP.NET Scaffolding to generate the controllers and views.

STEP 1 -  Create new project on Visual Studio 2015

Open Visual Studio

Create new ASP.NET Web Application Project

Give a name to the project (in this case I call him MVC5)

Select Template MVC

https://code.msdn.microsoft.com/site/view/file/144481/1/1.PNG

 

STEP 2 -  Create class Movies

Add new class to the project (this class represents a table, and the properties the colums of that table)

Give a name to that class (in my sample I call him MoviesModel)

On the class create the following code:

C#

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.

https://code.msdn.microsoft.com/site/view/file/106307/1/MVC52.jpg

 

STEP 3 -  Create controller with views using Entity Framework

Add new Scaffold to the project (new item existent on MVC5)

https://code.msdn.microsoft.com/site/view/file/106308/1/MVC53.jpg

  Choose option MVC5 Controller with views using Entity Framework

https://code.msdn.microsoft.com/site/view/file/144482/1/2.PNG

Click Add

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 scaffold item again.

https://code.msdn.microsoft.com/site/view/file/144483/1/3.PNG

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.Threading.Tasks; 
using System.Net; 
using System.Web; 
using System.Web.Mvc; 
 
namespace MVC5.Models 
{ 
    public class MoviesController : Controller 
    { 
        private MovieDBContext db = new MovieDBContext(); 
 
        // GET: Movies 
        public async Task<ActionResult> Index() 
        { 
            return View(await db.Movies.ToListAsync()); 
        } 
 
        // GET: Movies/Details/5 
        public async Task<ActionResult> Details(int? id) 
        { 
            if (id == null) 
            { 
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
            } 
            Movie movie = await db.Movies.FindAsync(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 async Task<ActionResult> Create([Bind(Include = "ID,Title,ReleaseDate,Genre,Price")] Movie movie) 
        { 
            if (ModelState.IsValid) 
            { 
                db.Movies.Add(movie); 
                await db.SaveChangesAsync(); 
                return RedirectToAction("Index"); 
            } 
 
            return View(movie); 
        } 
 
        // GET: Movies/Edit/5 
        public async Task<ActionResult> Edit(int? id) 
        { 
            if (id == null) 
            { 
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
            } 
            Movie movie = await db.Movies.FindAsync(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 async Task<ActionResult> Edit([Bind(Include = "ID,Title,ReleaseDate,Genre,Price")] Movie movie) 
        { 
            if (ModelState.IsValid) 
            { 
                db.Entry(movie).State = EntityState.Modified; 
                await db.SaveChangesAsync(); 
                return RedirectToAction("Index"); 
            } 
            return View(movie); 
        } 
 
        // GET: Movies/Delete/5 
        public async Task<ActionResult> Delete(int? id) 
        { 
            if (id == null) 
            { 
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
            } 
            Movie movie = await db.Movies.FindAsync(id); 
            if (movie == null) 
            { 
                return HttpNotFound(); 
            } 
            return View(movie); 
        } 
 
        // POST: Movies/Delete/5 
        [HttpPost, ActionName("Delete")] 
        [ValidateAntiForgeryToken] 
        public async Task<ActionResult> DeleteConfirmed(int id) 
        { 
            Movie movie = await db.Movies.FindAsync(id); 
            db.Movies.Remove(movie); 
            await db.SaveChangesAsync(); 
            return RedirectToAction("Index"); 
        } 
 
        protected override void Dispose(bool disposing) 
        { 
            if (disposing) 
            { 
                db.Dispose(); 
            } 
            base.Dispose(disposing); 
        } 
    } 
} 

b. The Views were automatically created.

https://code.msdn.microsoft.com/site/view/file/106310/1/MVC55.jpg

  Run the application

https://code.msdn.microsoft.com/site/view/file/144484/1/4.PNG

https://code.msdn.microsoft.com/site/view/file/144485/1/5.PNG

MVC5 Resources

Some good online resources about MVC5:

ASP.NET MVC5 : The official Microsoft .NET WebSite
http://www.asp.net/mvc/tutorials/mvc-5

For more examples, follow my blog at http://joaoeduardosousa.wordpress.com/

Check my other source code: