Click here to Skip to main content
13,248,927 members (42,827 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 16 Aug 2009

Entity Framework with ASP.NET MVC

, 16 Aug 2009
Rate this:
Please Sign up or sign in to vote.
In this article, I will explore the entity framework with ASP.NET MVC. We will modify the form validation sample that will access the data layer using the Entity Framework.


In this article, I will explore the Entity Framework with ASP.NET MVC. We will modify the form validation sample that will access the data layer using the Entity Framework.

We will create the following table in a local database using SQL Express:

CREATE TABLE [dbo].[tblComment]( 
    [CommentID] [int] IDENTITY(1,1) NOT NULL, 
    [Name] [nvarchar](50) NULL, 
    [Email] [nvarchar](100) NULL, 
    [Comment] [nvarchar](100) NULL)

Now, right click on the Models directory and select Add New Item. In the dialog, select ADO.NET Entity Data Model, as shown below:

In the wizard, select "Generate from Database", and then select your database connection string. Then, select the tblComment table and the click Finish. Visual Studio will create a set of .NET classes that are custom built for accessing the database, as shown below:

Now, we will modify our controller as shown below:

public class UserCommentController : Controller 
    public ActionResult UserComment() 
        return View(); 
    public ActionResult UserComment(string name, string email, string comment) 
        ViewData["name"] = name; 
        ViewData["email"] = email; 
        ViewData["comment"] = comment; 
        if (string.IsNullOrEmpty(name)) 
            ModelState.AddModelError("name", "Please enter your name!"); 
        if (!string.IsNullOrEmpty(email) && !email.Contains("@")) 
            ModelState.AddModelError("email", "Please enter a valid e-mail!"); 
        if (string.IsNullOrEmpty(comment)) 
            ModelState.AddModelError("comment", "Please enter a comment!"); 
        if (ViewData.ModelState.IsValid) 
            // Add to database 
                CommentEntities _db = new CommentEntities (); 
                tblComment commentToCreate = new tblComment(); 
                commentToCreate.Name = name; 
                commentToCreate.Email = email; 
                commentToCreate.comment= comment; 
                return View(); 
        return View(); 


In this article, we explored the Entity Framework with ASP.NET MVC. In the next article, I will explore a custom IErrorDataInfo interface.


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


About the Author

Farooq Kaiser
Software Developer (Senior)
Canada Canada
12+ years of complete software development life cycle experience for web based applications and multi-tier client-server desktop, primarily using LINQ, WCF, WWF, C#, ASP.NET, XML, XSLT, AJAX, Winforms,Visual Basic, JavaScript, JQuery, Google APIs, C++, VB.NET, C, ATL/COM, Open XML. Extensively involved in the requirement analysis, feasibility study, conceptualization, planning, architecture/design, configuration, development, quality assurance, implementation and release of the software products.

You may also be interested in...

Comments and Discussions

General Pin
Nirosh22-Jul-10 19:13
memberNirosh22-Jul-10 19:13 
GeneralMy vote of 1 Pin
Shawn Camp17-Aug-09 5:03
memberShawn Camp17-Aug-09 5:03 
GeneralMy vote of 1 Pin
Henry Minute17-Aug-09 1:17
memberHenry Minute17-Aug-09 1:17 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 16 Aug 2009
Article Copyright 2009 by Farooq Kaiser
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid