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# (538.1 KB)
540,295 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • IncompatibleException in Entity framework
    1 Posts | Last post November 23, 2016
    • Am using vs 2015. When i tried to run this project, it is giving the error : An exception of type 'System.Data.ProviderIncompatibleException' occurred in EntityFramework.dll but was not handled in user code.
      So i tried in PMC : update-database , go again below exception : 
      Exception calling "CreateInstanceFrom" with "8" argument(s): "Could not load file or assembly 'EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' 
      or one of its dependencies. The system cannot find the file specified."
      At E:\Practice Projects\Getting Started with Entity Framework 5 using MVC 4\C#\packages\EntityFramework.5.0.0\tools\EntityFramework.psm1:431 char:5
      i have changed the connecting string from v11.0  to MSSQLLocalDB.
      Please let me know what i have to do to solve this issue. 
  • Is there any possibility to run this solution in VS2012?
    2 Posts | Last post October 17, 2016
    • Hi Tom,
        Thank u for this instruction.
      I have VS2012 only. Is there any possibility to run this with some update? 
    • It might but I haven't validated that myself.  You could use the free VS 2015 Community Edition to do the tutorial, while continuing to use VS 2012 in your other work.  Everything you learn about EF and MVC would apply in VS 2012 as well as 2015.
  • Creating DB using VS2015
    5 Posts | Last post October 17, 2016
    • Using Visual Studio Community 2015RC the app could not create and load the database.
      As explained in this link starting with this VS2015, the team is moving away from version dependent connection strings. That means, once you adopt VS 2015, you have the option of using a version independent  connection string moving forward. 
      To fix this issue for now, and future versions of Visual Studio, replace the version dependent connection string (e.g. “v11.0”) with “MSSQLLocalDB”.
      So open the Web.config file and change the connectionString as follows:
      connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity2;Integrated Security=SSPI;"
      connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=ContosoUniversity2;Integrated Security=SSPI;"
    • Great example !
    • Great
    • That's great.Thanks
    • Nice!
  • Relationship between Instructor and OfficeAssignment
    2 Posts | Last post September 23, 2016
    • Hi Tom, 
      Thank you for all the detaill instructions. 
      I have one query that why it's   
       public virtual OfficeAssignment OfficeAssignment { get; set; } 
      , rather than  public virtual ICollection<OfficeAssignment> OfficeAssignment { get; set; } 
      in Instructor class, since it's 1:N relationship between instructor and officeassignment.
    • The relationship is 1 to zero or 1; in this model you can't have multiple office assignments for an instructor.
  • Get Error when access student page
    2 Posts | Last post September 09, 2016
    • Hi Tom, I get error when access student page
      Exception Details: System.ComponentModel.Win32Exception: The system cannot find the file specified
      Source Error:
      Line 19:         public ActionResult Index()
      Line 20:         {
      Line 21:             return View(db.Students.ToList());
      Line 22:         }
      Line 23: 
      Source File: c:\users\juang\documents\visual studio 2015\Projects\ContosoUniversity\ContosoUniversity\Controllers\StudentController.cs    Line: 21 
      I'm new in .NET MVC, please help me to can resolve the error
    • Make sure you follow all of the instructions under the Getting Started section, including the PMC commands. If that doesn't help try following the tutorial to see what happens when you build from scratch, as that may help pinpoint where the error is coming from.
  • Identity 2.0 Plans?
    3 Posts | Last post August 02, 2016
    • Hi,
      I was wondering if there were any future plans of implementing login entity framework functionality using Identity 2.0 ?
      Thanks :-)
    • Probably not for this example.  The tutorial series is already quite long at 12 substantial tutorials, and a similarly long one could be done for identity itself.  The more topics you try to do in one tutorial, the less well you can do for any one of the topics.  For identity tutorials, see
    • It is interesting.
  • String.IsNullOrEmpty
    2 Posts | Last post January 05, 2016
    • I'm getting a syntax error at line 
      ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
      It's saying sortOrder does not exist in the current context.
    • Make sure you go to the home page and select the Student page from the tab rather than opening the Student page directly.
  • Running the application at the end of 1st tutorial
    2 Posts | Last post January 02, 2016
    • Hello, 
      I see an error when I run the application at the end i.e. after creating StudentController class which is as follows :
      ""An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
      Additional information: Failed to set database initializer of type 'SantosaUniveristy.DAL.SchoolDatabseInitializer, SantosaUniversity' for DbContext type 'SantosaUniveristy.DAL.SchoolContext, SantosaUniversity' specified in the application configuration. See inner exception for details.""
      Please help..
    • From the error messages that have Santosa instead of Contoso it appears that you've changed the code.  I recommend starting over - download the project again.  If you want to make changes it would be best to learn how by following the associated tutorial to build the application from scratch.
  • Running the application
    2 Posts | Last post December 31, 2015
    • Hi, I downloaded the code and build the solution successfully. But when i run the Update-Database command in the Package Manager Console, I got error "The term 'update-database' is not recognized as the name of a cmdlet,  function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. "
      I also tried changing the database name in web.config file , inside from v11.0 to mssqllocaldb - "" connectionString="Data Source=(LocalDb)\mssqllocaldb;.....""
      I am using Visual studio 2015.. Please suggest me the solution ..Thanks
    • I downloaded VS 2013 and its working . Thanks
  • VB.Net?
    2 Posts | Last post October 06, 2015
    • Is VB.Net no longer supported? Even the prominently placed samples such as this one which links right from the MSDN Home-->Samples page neglects VB.Net. 
      At one point in time, nearly all samples and documentation were provided in both languages. Lately there seems to be a significant trend of VB.Net neglect to the point I'm concerned. 
      You sold us VB.Net. We learned it, we like it. Please don't make us second class citizens for having drank your Kool-Aide. 
      We suffer enough second class citizenship as it is for having chosen Microsoft platforms which significantly lack in the area of open source LOB development and efficient LOB development tooling. I doubt one could keep track of the number of Open source ERP and CRM systems on LAMP. There are exactly two on the Microsoft Stack and ORM and neither are well architected, stable and SQL Server based.
    • VB.NET is still supported; in some cases we only have time and resources to provide a given sample in one language, and that is often C#. For this particular sample there is a VB version of both the tutorial and the downloadable project, provided by Mike Brind -- see
1 - 10 of 68 Items