ASP.NET MVC 4 – CRUD operations with Entity Framework (8 steps to create your site)

This article walks you through using ASP.NET Scaffolding to generate controllers and views. 

Introduction

Using MVC and Entity Framework, 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.  Unless you have very simple requirements, the scaffolded application is probably going to be a prototype rather than complete and ready to deliver to end users.

STEP 1 - Create Project

Create a new project of type ASP.NET MVC 4 Web Application  

http://code.msdn.microsoft.com/vstudio/site/view/file/96426/1/Picture1.png

Choose the option Internet Application

http://code.msdn.microsoft.com/vstudio/site/view/file/96427/1/Picture2.png

After project creation, it look like this:

 http://code.msdn.microsoft.com/vstudio/site/view/file/96429/1/Picture3.png

STEP 2 - Add Items

Add new items of type ADO.NET Entity Data Model to the solution.

Name it to Model.edmx

 http://code.msdn.microsoft.com/vstudio/site/view/file/96430/1/Picture4.png

STEP 3 - Create Model

Open the Model.edmx, with double click on the top of the file and create the data structure.

http://code.msdn.microsoft.com/vstudio/site/view/file/96432/1/Picture5.png

 

On this sample, we have two entities (Clients and Contacts).

After save this model, two new classes will be generated (Clients.cs and Contacts.cs)

 

Clients:

 http://code.msdn.microsoft.com/vstudio/site/view/file/96433/1/Picture6.png

Contacts:

http://code.msdn.microsoft.com/vstudio/site/view/file/96434/1/Picture7.png

 

STEP 4 - Install Nuget

On the package manager console, execute the follow command.

Install-package mvcscaffolding

http://www.nuget.org/packages/MVCScaffolding/** **

http://code.msdn.microsoft.com/vstudio/site/view/file/96435/1/Picture8.png

STEP 5 - Scaffold Controllers

Now we can scaffold a controller and a set of Create, Read, Update and Delete (CRUD) views. In the Package Management console run the following command.

Scaffold controller ClientsController –force –DBContextType “ModelContainer”

Scaffold controller ContactsController –force –DBContextType “ModelContainer”

This will generate a set of views, a controller and an Entity Framework database context.

http://code.msdn.microsoft.com/vstudio/site/view/file/96436/1/Picture9.png

 

On the next image, you can see the files created by the command executed.

The repositories, controllers and views.

http://code.msdn.microsoft.com/vstudio/site/view/file/96437/1/Picture10.png

STEP 6 - Change ModelContainter

Comment the line //throw new UnintentionalCodeFirstException()

 http://code.msdn.microsoft.com/vstudio/site/view/file/96438/1/Picture11.png

STEP 7 - Change Menu

Create two new entries on the application menu.

One to call the Clients entities and other to call Contacts entities. This could be made on the /Views/Shared/_Layout.cshmtl.

 

http://code.msdn.microsoft.com/vstudio/site/view/file/96439/1/Picture12.png

STEP 8 - Run Application

Run the application.

Verify that two new entries exists on menu and that you can Create/Edit/Delete both entities created.

 http://code.msdn.microsoft.com/vstudio/site/view/file/96440/1/Picture13.png

http://code.msdn.microsoft.com/vstudio/site/view/file/96441/1/Picture14.png

MVC4 Resources

Some good online resources about MVC4: 

Code Samples

All of this sample can be found and downloaded in Microsoft Code Gallery: