MVC4 – Entity Framework

8 Steps to create your site

 

STEP1

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

Choose the option Internet Application

After project creation, it look like this:

 


STEP2

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

Name it to Model.edmx

 


STEP3

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

 

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:

 

Contacts:

 

STEP4

On the package manager console, execute the follow command.

Install-package mvcscaffolding

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

STEP5

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.

 

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

The repositories, controllers and views.



STEP6

Comment the line //throw new UnintentionalCodeFirstException()

 


STEP7

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.

 



STEP8

Run the application.

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