Wednesday, April 24, 2013

Create an application using MVC 4 and EF code first

In this tutorial we will create our first MVC application using visual studio 2012, MVC 4 and EntityFramework Code First.

Step-1: Create Project
1. In Visual Studio 2012, add a new project by selecting the File menu, then New and then Project.
2. Select ASP.NET MVC 4 Web Application and C# as a language of choice.
3. Name the project Inventory and then click OK.
4. In the New ASP.NET MVC 4 Project wizard, choose Internet Application and razor view engine.
5. Leave the default values of the other options and click OK.

Step-2: Create a model class
1. In Solution Explorer, right click the Models folder.
2. Select Add from the menu and then from the bottom of its context menu choose Class.
3. In the Add New Item dialog, change the new class name to Category.
4. A new file, Category will be created.

Add following code into category class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;

namespace Inventory.Models
    public class Category
        public int CategoryID { get; set; }

        [DisplayName("Category Name")]
        public string CategoryName { get; set; }

Now, We’ll create a class that inherits from DbContext and knows how to serve up and manage the Category object. The Entity Framework will take care of bridging the classe and a database. Before the use of DbContext, we’ll need to create a reference to the Entity Framework API. This is the assembly that contains the Code First runtime. Visual studio 2012 automatically creates a reference of Entity Frmework. But for VS 2008 or 2010 we need to create a reference of EF by using Nuget. Nuget allows us to easily find and install reference assemblies from the internet.

1. Select the Inventory project in Solution Explorer.
2. From the Tools Menu, choose Library Package Manager which has a sub-menu.
3. From the sub-menu choose Package Manager Console.
4. At the console’s PM prompt type install-package EntityFramework then hit enter.
When the package is installed, you should see the “success message”.

Add following class to the Models folder and compile the application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace Inventory.Models
    public class InventoryContext: DbContext
        public DbSet<Category> Categories { get; set; }

Step-3: Create Controller
1. Build the project by choosing Build from the Visual Studio menu and then Build Inventory from its drop-down menu.
2. In the Solution Explorer, right click the Controllers folder.
3. Click Add from its context menu and then Controller.
4. In the Add Controller window, change the Controller Name to CategoryController and select MVC Controller with read/write and view using EntityFramework.
5. Select Model Class: Category(Inventory.Models).
6. Select DBContext Class: InventoryContext.Models).
7. Views: Razor(cshtml).
Click the Add button.
The CategoryController class will be created with ActionResult methods: Index, Details, Create, Edit and Delete.

Step-4: Change the global.asax
1. Open the global.asax file from Solution Explorer.
2. Modify the MapRoute call to change the value of controller from “Home” to “Category”.
   "Default", // Route name
   "{controller}/{action}/{id}", // URL with parameters
   new { controller = "Category", action = "Index", id = UrlParameter.Optional }

Our first application is created. Run the application. The CategoryController.Index action will be the first method called.


Post a Comment