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

  • Null reference when saving a student with non-existing ID
    3 Posts | Last post June 12, 2015
    • Hi there,
      I found an issue in Contoso University. It is possible to tamper the HTTP Post when saving a student and provided a non-existing ID. As a result, the lookup will return a null response that fails.
      It is necessary to verify the object. I wrote a blog post here
    • That's not an issue. If you think this is important, you could check the ID before you post.
    • It is an issue, the code does not check if the studentToUpdate object is null.
      Note that this code is also vulnerable to 'Insecure Direct Object References'
      I should not be allowed to modify a student just by changing the student ID, I would expect a HTTP 401 Not Authorized   :).
  • Creating DB using VS2015
    1 Posts | Last post June 05, 2015
    • 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;"
  • Why i can't see navbar-toggle display
    2 Posts | Last post June 04, 2015
    • I see in _Layout.cshtml have declare navbar-toggle button, but when i run app, it doesn't appear
    • That is code from the Visual Studio new-project template that I don't make use of in this sample application.
  • Getting a NuGet Package restore failed error
    2 Posts | Last post May 15, 2015
    • I've downloaded and extracted the files. Then I opened it in VS 2013 and built it. Only it failed with 187 errors. The first one, which I suppose it at the route of it all, reads as follows:
      "NuGet Package restore failed for project ContosoUniversity: The request was aborted: Could not create SSL/TLS secure channel.."
    • Well, I tried building it a second time, and this time it worked. Don't know why, but whatever.
      Thanks anyway.
  • Can't create database
    3 Posts | Last post May 12, 2015
    • Hi, I downloaded the code and i builded the solution successfully. But when i run the Update-Database command in the Package Manager Console, i got an error 'Cannot find the object "dbo.Student" because it does not exist or you do not have permissions' .
      How can i do?
    • Try changing the database name in web.config and re-running the update-database command.
    • I am according to the tutorial created a database with the same name before download the code, Problem is solved after i delete the database. Thanks for your help. 
  • MVC6
    2 Posts | Last post March 02, 2015
    • Very helpful resource! Are there plans to update it to MVC6?
    • Yes there are plans, but MVC6 is still changing rapidly, and long tutorial series like the one this is based on are time-consuming to revise, so it's usually not practical while a product is still in preview/beta phases.  The tutorial and project most likely won't be updated until the MVC6/EF7 is released and thus relatively stable.
  • No dependency injection used?
    2 Posts | Last post January 19, 2015
    • Hi,
      I downloaded and trying to get around with the Contoso University sample. So as I understand no additional repositories and unitofwork patterns need to be implemented since EF 6 incorporates these? What about the dependency injection? I don't see the dependency injection implemented here. why? Thanks.
    • The tutorial series and the sample app it creates isn't a comprehensive introduction to programming best practices; its purpose is to teach EF with MVC, so it avoids adding in other topics that might divert attention from EF/MVC. Mike Wasson has a great introduction to DI with Web API, and the principles for MVC are similar:
  • Where is the source code to the VS 2010 MVC 3 version?
    8 Posts | Last post January 10, 2015
    • 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.
    • i also used thanks for sharing, is it true on my website
  • how to add JQuery autocomplete
    2 Posts | Last post January 08, 2015
    • i have tired to add Jquery autocomplete to the Student page but i keep getting Jquery undefined errors.  i think i have all the paths correct but yet it wont work. any ideas?
      PS. what a fabulous demo app buddy. well done.
      View .................
      <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
      <script src="~/Scripts/jquery-ui.js"></script>
      <script src="~/Scripts/jquery-1.10.2.js"></script>
          ViewBag.Title = "Students";
      <script type="text/javascript">
              $(function () {
                      source: '@Url.Action("GetProducts")',
          @Html.ActionLink("Create New", "Create")
       public JsonResult GetProducts(string term)
                  List<string> Products = db.Students.Where(s => s.LastName.StartsWith(term))
                      .Select(x => x.LastName).ToList();
                  return Json(Products, JsonRequestBehavior.AllowGet);
    • Did you add the jQueryUI NuGet package?  That's the only issue with the code that I can think of, if you already did that you might try StackOverflow for other ideas.
  • Won't download
    1 Posts | Last post December 24, 2014
    • ISU
      IE 11.0.96
      When I click on the download link it does not matter whether I choose: save, save as, or open because nothing happens.
1 - 10 of 57 Items