Click here to Skip to main content
Click here to Skip to main content

ASP.NET MVC3 Controller with Entity Framework CodeFirst

, 27 May 2011
Rate this:
Please Sign up or sign in to vote.
ASP.NET MVC3 Controller with Entity Framework CodeFirst

We already discussed about the Controllers and Routers in ASP.NET MVC 3. In this article, we will look a little deeper into Controllers and Razor Views.

Controller with CRUD Operations

Let us define our Model first. For our sample, we will define a sample Model class.

public classSampleModel
    {
        publicint Id { get; set; }
        publicstring FirstName { get; set; }
        publicstring LastName { get; set; }
        publicint Age { get; set; }
    }

Now let us add a new Controller. Right click on the Controllers folder and select Add-> Controller.

Add a new Controller

Add a new Controller

This will open the Add Controller Window. Specify the
Controller name as SampleController and select the Scaffolding options.

For our sample, select the Template as ‘Controller with read/write actions and views, using Entity Framework’. This will add actions for all operations like edit, list, etc.

Add Controller

Add Controller

Select our SampleModel under Model class. Select New DataContext option in Data context class and specify the name for the new data context.

Add Controller

Add Controller

Select the Views as Razor.

Note: MVC supports two types of view engines; one is the Web form view engine and another is the Razor View engine. Web form view engine works with aspx/ascx files and razor view engine work with cshtml file. We will discuss more about Razor views latter.

This will create the SampleController, which supports all the CRUD operations. It also generates the Sample folder and views under the Views folder.

Solution Explorer

Solution Explorer

Let us run the application and verify the Sample pages.

Your Sample controller page:

Sample Page

Sample Page

Note that the page is constructed with data from your Model class. Also, it allows us to add data to the SampleModel. Click on Create New link.

This will open the Create action view and allows us to enter data.

New Record

New Record

Now, it will display the entered data with edit, delete and details options.

Sample Page

Sample Page

Clicking on Details will display the detailed view of the record.

Details Page

Details Page

Now, close the application and re-run the same. You may be surprised to see the data entered earlier displayed in the web application.

Now the question is where is the data saving? The answer is CodeFirst approach introduced as part of Entity Framework. For more details on CodeFirst Approach, please refer to my article, EntityFramework-CodeFirst Approach.

Open the server explorer and connect to the SqlExpress. Notice that we have a new database in sqlexpress with the name SampleApp.Context.SampleAppContext.

New Connection

New Connection

Connect to the new database and observe that we have a new table with SampleModels with the data, which we entered in the web application UI.

SampleModels

SampleModels

Filed under: ASP.Net, Entity Framework, MVC Tagged: ASP.Net, ASP.Net MVC 3, EntityFramework, MVC

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

ambilykk
Architect TCS
India India
I have over 10 years of experience working on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP.Net, HTML5, jQuery and Ajax. My interests also include TFS, Azure, Windows 8, Visual Studio 2010. Technology adoption and learning is my key strength and technology sharing is my passion.
 
Microsoft MVP: Connected Service Developer
Follow on   Twitter   LinkedIn

Comments and Discussions

 
QuestionHow to Make these views as dialog form? Pinmemberarshanmugam28-Feb-13 0:03 
QuestionCould you please tell me where the connection string to my database is? PinmemberYoyosch9-Aug-12 19:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 27 May 2011
Article Copyright 2011 by ambilykk
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid