Saturday, September 22, 2012

How to create an ASP.NET MVC 4 Application with Entity Framework

Open Visual Studio and create a new “ASP.NET MVC 4 Web Application” either in C# or but I am using VB.NET and Visual Studio 2012 Professional.  You can build this using Visual Studio 2012 Express for Web too.

Select “Internet Application” as the Project Template, Razor as View engine, and if you like to create test you can have “create a unit test project” option checked.  Click ok.

After you hit ok, you can see ASP.NET has created several folders for us.  It has also brought several scripts that are used in modern web application development. I always wanted to switch over to ASP.NET MVC from Web Forms as to do some things it was really hard to do in Web Forms as your application go big.  ASP.NET MVC 4 does most of the work for you like Bundling Javascript, little things like adding scripts at the bottom of the page and more.

For data I am using Adventure Works 2008 database which can be easily downloaded from Codeplex website (link).  We have to create a data model for our web application and we are going to use ADO.NET Entity Framework Model.  So let’s add an ADO.NET Entity Data Model to our Models Folder by right clicking on it.

Specify “myModel” as Name for the item which will create myModel.edmx file in our Models Folder.

On Entity Data Model Wizard, Select “Generate Model from database” and click on Next.

On Choose your Data connection and then Click on New Connection

I used the (.) symbol to specify localhost instead of my machine name however either approaches should work.

It will create Entity Connection String, leave it as it is and Click on Next.

On Choose Your Database Objects and Settings expand the Tables object and expand Production and select everything but the “Document” table otherwise it will give you the following error after you click Finish.

Unable to generate the model because of the following exception: ‘The table ‘AdventureWorks2008.Production.Document’ was referenced by a relationship, but was not found

Click on Finish and you will see myModel.edmx file opened and a diagram will show you our relational tables.

Notice in the Error List though, there are several errors which at first would scare away a newbie. There is nothing to worry about just build the project and those errors should go away.

We want to add Controller which will act as a bridge between our Model and View.  So add a New controller class by right clicking on the Controllers folder.

Type ProductsController as Controller name and Click on Add.

Add the following code where we create a new Context object named as db.  Notice AdventureWorks2008Entities is the name of my connection string.

Modify the default Index function with the following linq query.  The linq query gets list of only 10 products whose list price is greater than zero.

Right click anwhere inside the index function and Add a View as shown in the figure below:

Create a View with just all these parametes as shown above.

Build the project and browse in the Internet Explorer at the following location.

You will see a list of 10 products on this page.  In the next post we will see how to select an individual product item with keyboard and hit enter to see the details of this product on a details page.