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)
 
 
 
 
 
(123)
325,594 times
Add to favorites
12/8/2014
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • MVC6
    2 Posts | Last post Mon 5:45 PM
    • 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:
      http://www.asp.net/web-api/overview/advanced/dependency-injection
      
      
  • 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: 
      
      http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=aspnetmsdnexamples&DownloadId=16474
      
      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 http://code.msdn.microsoft.com/Getting-Started-with-abeed7be.
    • @Cephas Lin -Thanks Cephas , I got the source code under your Samples.
    • i also used thanks for sharing, is it true on my website www.medyator.net
  • 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";
      }
      
      <h2>Students</h2>
      <script type="text/javascript">
              $(function () {
                  $("#txtSearch").autocomplete({
                      source: '@Url.Action("GetProducts")',
                  });
              });
      </script>
      <p>
          @Html.ActionLink("Create New", "Create")
      </p>
      
      controller..........
      
       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.
      
      https://code.msdn.microsoft.com/ASPNET-MVC-Application-b01a9fe8/file/130872/2/ASP.NET%20MVC%20Application%20Using%20Entity%20Framework%20Code%20First.zip
  • Database
    3 Posts | Last post December 17, 2014
    • Is there a SQL Script to create the database for this example?
      Thanks,
      David.
    • No, but you can use EF CF Migrations to create one. 
      http://msdn.microsoft.com/en-us/data/JJ591621.aspx#script
      
    • Sorry - I didn't read all the thread below. Looks like the answer was already provided.
      Got the DB as needed by changing the ConnectionString and executing the Update-Database command from the Package Manager Console.
  • How can you think out such a clever way?
    2 Posts | Last post November 05, 2014
    • Hi Tom,
      I'm learning this sample instructed by your post I found here "http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application". In the charpter "Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application" there's a snippet of code interests me a lot. Here it is:
      
      
      public ActionResult Index(string sortOrder)
      {
         ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
         ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
      var students = from s in db.Students
                        select s;
         switch (sortOrder)
         {
            case "name_desc":
               ...
            case "Date":
               ...
            case "date_desc":
               ...
            default:
               ...
         }
         return View(students.ToList());
      }
      
      It is just amazing! I like the way that you handled 4 different sorting scenarios by one parameter "sortOrder". I doubt that I could be able to think such a genius way out. Orz... I really want to know how did that come to your mind? But I think it's limited to handle such 4 scenarios just like the ones in your sample(sortOrder : "","name_desc","date_desc","Date"),also isnt it a little bit "tricky" ? I mean what if I have more than two columns of the grid support sorting? 
    • It's merely an example that shows one way to handle similar scenarios, how to use parameters in controller and view to change queries. Similar techniques can be used in a variety of scenarios, and for more columns or sort options you can investigate other options such as dynamic LINQ.
  • What kinds of knowledge should I know to fully understand your sample?
    3 Posts | Last post November 04, 2014
    • Hi Tom,
      What a great sample! This's exactly what I'm looking for for a long time! Thank you very much! And I do have questions like:
      1.What kinds of knowledge or skills should I get to help me fully understand your work? Since I just started to learn EF and MVC/WebApi, something looks really "unfamiliar" to me like ("PluralizingTableNameConvention",etc),so do you have any suggestion for me?
      2.Do you think it's necessary to introduce repository-pattern into this sample to make it more testable? Because in that case DA components like DbSet/DbContext could be replaced by mock objects easier(I googled a lot of articles, and the conclusion is that using repositories+EF is the most common way if I want to unit test my WebApi/MVC controller.I may be wrong on that).
      
      Thanks again!
    • There's a tutorial series that goes with the sample (that actually builds the sample from scratch)--see the links at the end of the Introduction on the Description tab.  The tutorials address the pluralizing question and the repository question. The EF 5 tutorial has a repository example, the EF 6 tutorial omits it because EF 6 has features that enable unit testing without your having to manually create a repository (see the note about this in the last tutorial in the EF 6 series).  Note that I am about to make a significant update to the sample, probably this week -- when it was written everyone including the EF team thought the Bind attribute could be used in Edit scenarios to prevent overposting but it was later determined not to be the case.  So HttpPost Edit methods will have different code in the new method; we're basically changing from using model-binder-created objects and setting status to modified, to re-reading the entity and calling TryUpdateModel to set properties.
    • Tom, 
      Thanks you very much! And during the past a few hours I've read some articles related to this article. Honestly I should notice them earlier before I ask my questions because they are already recommended by you in this post. Anyway, your answer is really appreciated, and I'll keep my eyes on your updates to this sample for sure. 
  • What kinds of knowledge should I know to fully understand your sample?
    1 Posts | Last post November 03, 2014
    • Hi Tom,
      What a great sample! This's exactly what I'm looking for for a long time! Thank you very much! And I do have questions like:
      1.What kinds of knowledge or skills should I get to help me fully understand your work? Since I just started to learn EF and MVC/WebApi, something looks really "unfamiliar" to me like ("PluralizingTableNameConvention",etc),so do you have any suggestion for me?
      2.Do you think it's necessary to introduce repository-pattern into this sample to make it more testable? Because in that case DA components like DbSet/DbContext could be replaced by mock objects easier(I googled a lot of articles, and the conclusion is that using repositories+EF is the most common way if I want to unit test my WebApi/MVC controller.I may be wrong on that).
      
      Thanks again!
  • IE11 browser data picker is not working
    2 Posts | Last post October 28, 2014
    • Why in my IE11 browser data picker is not working for Enrollment Date when I am trying to create a New Student.
    • The browser chooses the input control based on the data type. For the date type we're using Chrome provides a date picker but IE and Firefox don't. See 
      http://html5tutorial.info/html5-date.php
      
1 - 10 of 52 Items