ASP.NET MVC Application Using Entity Framework Code First

A Visual Studio 2013 project which shows how to use the Entity Framework 6 in an ASP.NET MVC 5 web application project, using the Code First development approach.

C# (913.1 KB)
271,701 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • Where is the source code to the VS 2010 MVC 3 version?
    7 Posts | Last post September 02, 2014
    • I have the E-Book "Getting Started with the Entity Framework 4.1 Using ASP.NET MVC".pdf which states that the requirements are VS 2010 SP1.  I've fulfilled that and the other requirements.  However, the link "download the completed application" brings me HERE and the requirements to create, build and run the sample are now different.  
      So, where is the source code to the VS 2010 MVC 3 version?  I don't WANT to build the "latest" version - for a SPECIFIC REASON.  
    • Here's a link to the C# MVC 3 project: 
      We forgot to re-post the old version somewhere when we updated the sample for MVC 4 EF 5.
    • Thank you, Tom.  Much appreciated.
    • Hi Tom,
      I was aslo looking for the MVC 3 version sample code and tried the URL you have mentioned here. But it is not available there. So can you please share the correct link to download MVC3 with EF 4.1 sample code.
      Hi vedicwarrior
      If you got the sample, please share it, this would be really helpful.
    • @kpant, I'm sorry but the site where we were hosting the download was retired rather suddenly without our team getting enough advance notice to find an alternative. We're working on getting the files back and finding a new hosting location and when that happens I'll provide a new link.
    • @kpant - The MVC3 sample is now hosted at
    • @Cephas Lin -Thanks Cephas , I got the source code under your Samples.
  • Runtime parser error
    5 Posts | Last post August 23, 2014
    • When I try to run this application in Visual Studio 2013, I get the following parser error. Please help!!!
      Parser Error 
        Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 
       Parser Error Message: Could not load type 'ContosoUniversity.MvcApplication'.
      Source Error: 
      Line 1:  <%@ Application Codebehind="Global.asax.cs" Inherits="ContosoUniversity.MvcApplication" Language="C#" %>
       Source File:  /global.asax    Line:  1 
    • Did all of the NuGet packages get restored successfully, and does the application build without errors? This project shouldn't require Update 3 (the most current update), but when you run into difficulties it's generally a good idea to make sure that your copy of Visual Studio is current.
    • Tom,
      I applied the Update 3 to Visual Studio 2013 as you suggested and now the earlier error does not appear but it doesn't show the correct start page with the student listing as shown in the example. Instead it shows a page in the browser that says Welcome to Contoso University and has options to "Build it from scratch" and "Download it". Nothing like the Student listing screen shown in the sample file screenshots.
      Any idea what's the problem?
    • You should see tabs (links) to the Student, Instructor, and other pages at the top of that home page. If you have a narrow browser window, you'll have to click the square box of short horizontal lines at top right to open up the tabs menu.
    • Thanks for your sharing. It's very useful for my site
  • UnitOfWork only in CourseController
    4 Posts | Last post April 20, 2014
    • I found that you are using UnitOfWork only in CourseController like
      private UnitOfWork unitOfWork = new UnitOfWork();
      And in rest controllers you are directly using DbContext.
      Why is this?
      It will be good if could you modify the code with Dependency Injection or IOC container?
    • The downloadable project is exactly as it would be created if you worked through the tutorial. The UnitOfWork class is added in one of the later tutorials as an example of how to implement that pattern, and having shown the example there was no need to go back and add it to controllers that had been created earlier.
      Thanks for the suggestion, we'll consider adding dependency injection in a later edition of the tutorial.
    • I agree, this is the way to go. Nice sample!
    • helpful, thanks!
  • CU10 database
    2 Posts | Last post February 07, 2014
    • I download your project example. when I click the page, it show can't find dbo.student. 
      I try to look database table but I don't see that student table, just see Course, CourseInstructor, Department, Enrollment, OfficeAssignment and Person.
      what happen? 
    • Try changing the database name in Web.config and running update-database again.
  • unique key
    2 Posts | Last post January 22, 2014
    • Hi Tom how can I manage unique constraint please.
    • For questions not about the sample/tutorial itself, see StackOverflow or the ASP.NET EF forum (links at the top of the first page in the tutorial series).
  • documentation
    2 Posts | Last post January 06, 2014
    • hi Tom can i please have the documentation to c how the code flows please please its working perfect
    • See the Description tab -- there's a link to the tutorial series that shows how to build the application from scratch.
  • documentation
    1 Posts | Last post January 05, 2014
      report please Tom
  • Attach file to database
    2 Posts | Last post January 03, 2014
    • Hi Tom:
      I can't attach file CU10.mdf to database.
      Do you have any script to do that?
    • If you mean update-database creates the file but it won't attach, chances are you already used the CU10 name. Go into Web.config and change the name of the database or delete the existing database.  For instructions on how to change the name, see the note near the end of the fourth tutorial:
  • Do you attached database with this application?
    2 Posts | Last post December 20, 2013
    • HI Tom :
       Can't find database CU10.mdf......App_Data folder is empty
    • In the Description under Getting Start there are build and run instructions -- one of the steps is to run the Update-Database command.  That creates the database; the database .mdf file isn't included in the download.
  • CU10.mdf
    3 Posts | Last post December 01, 2013
    • Hi Tom:
      When I tried to run your application, everything worked until it tried to attach the file CU10.mdf to the database whereupon it choked.  
      I couldn't find this file anywhere in the download.  Am I missing something?
      Roger S 
    • Whoops - I guess my local system doesn't handle attach well - removed that from the connection strings and database built like a charm - thanks - looking forward to going though this.
    • I too encountered this error in the Package Manager after running 'Update-Database' to set up the project. The error I received was:
      Cannot attach the file 'C:\Users\Rich\Documents\Visual Studio 2013\Projects\EFCF\C#\ContosoUniversity\App_Data\CU10.mdf' as database 'CU10'.
      The reason for this was that I had previously set the solution up and an instance of the CU10 database was already attached. 
      To resolve I logged into the '(LocalDB\v11.0)' dataabse in SQL Server Management Studio and deleted the CU10 database that was there. I reran the command and all is well once again in LaLa Land. 
1 - 10 of 42 Items