MVC4 – Entity Framework

8 Steps to create your site



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

Choose the option Internet Application

After project creation, it look like this:



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

Name it to Model.edmx



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)







On the package manager console, execute the follow command.

Install-package mvcscaffolding 


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.


Comment the line //throw new UnintentionalCodeFirstException()



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.



Run the application.

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