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# (541.9 KB)
4.4 Star
671,006 times
Add to favorites
E-mail Twitter Digg Facebook
  • IncompatibleException in Entity framework
    3 Posts | Last post March 27, 2019
    • 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. 
    • This sample project is for EF 6 and MVC 5.
    • Can you try cleaning up the temp files and repair the VS installation.
      One scenario, it will help correct the adapter settings
      Editor at
  • Enrollment
    5 Posts | Last post March 27, 2019
    • Tom,
      Nice demo. The enrollment does not show up on the nav bar , when I add it or try to access it by http://localhost:41787/Enrollment I get an error" HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable" I see there is an enrollment class but no controller. I tried adding the controller  still get the error. Any suggestions?
    • The sample app doesn't have an enrollments page.  See the tutorial linked to on the description page for samples of the pages included in the project and instructions for how to add controllers and views.
    • Thanks Tom
      I created one with a controller . It works great. Excellent sample just what I need to learn many to many relations.
    • I also created EntrollmentController and related views (.cshtml pages). It works perfectly fine. 
      Got very good help in understanding the EF and MVC concepts. Thanks a ton !!
    • I created one with a controller . 
  • Creating DB using VS2015
    9 Posts | Last post October 15, 2018
    • 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!
    • That's exactly what I was looking for, thank you
    • love it
    • Thanks!
    • I agree with MrSparc, the connection string should be independent from SQL Server versions. I recently downloaded this sample source, and found out that it needed SQL Server 2012, which I do nit use. The sample's instructions did not help resolve this problem.
      I have several versions of SQL Server on my development PC. While 'Data Source=(LocalDb)\MSSQLLocalDB' works well and is simple, (1) I cannot find the .mdf DB file, (2) I cannot choose the SQL Server version, and (3) I cannot bring it up and query the data on SQL Server Management Studio.
      For those in my position, I suggest being more specific in the connection string, for example 'Data Source=DESKTOP-V6XX481\SQLEXPRESS01'; i.e., your PC name for the server name, and the SQL Server connection name for the version. If you don't know what this is, run SQL Server Management Studio, on the login screen drop down the list of servers and browse for all available servers.
      This sample seems to be creating the tables in the database you selected when you first run it. Before the first run, I created 'ContosoUniversity' database on the server I wanted to use, and the app did the rest.  BTW, I dropped the '2' at the end of the name. I was expecting the app to insert the sample data on the database, but it did not.
    1 Posts | Last post October 12, 2017
  • git repository
    2 Posts | Last post September 08, 2017
    • Do you have this in git in order to create a branch ? I would like add more feature for this demo in order to be used for academic purpose .
    • So do I
  • Uploading a multi file on visual studio mvh-code first
    2 Posts | Last post September 08, 2017
    • hello,
      When u upload let's say 4 picture at the same time, only one of
      The pictures goes in to the database and the rest of
      The pictures end up in the right folder in the project. How can I get all of them
      In the database? How should the table look like? 
      Hoop that you guys understand the question and can help me putt with this it's really urgent.
    • This logic is a bit complicated
  • Git repository
    1 Posts | Last post September 08, 2017
  • error while display student pages
    2 Posts | Last post January 07, 2017
    • Hello,
      I already try but get some error when i open student pages like this:
      'A network-related or instance-specific error occurred while establishing a connection to SQL Server'.
      I actually download sql server management studio but still can't connect to database 
      How to solve it?
    • Hi
      It sounds like the database tables have not populated. What version of Visual Studio are you using?
      A couple of things:
      1. Can you open the pages when you run locally? If not, then the database isn't being created and you need to make sure you follow the instructions on using the Nuget Package manager to restore packages and then close \ start Visual Studio and run update-database from the NPM console (see description tab). Also, make sure you have updated the web.config accurately. It should be:
          <add name="SchoolContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=ContosoUniversity2;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
      2. If so, and it is an Azure issue, make sure you have enabled the firewall on the Azure SQL Server for your client IP address.
  • 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.
  • 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.
1 - 10 of 74 Items