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)
 
 
 
 
 
(132)
364,283 times
Add to favorites
3/9/2015
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • 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
      
  • Where is the tutorial?
    3 Posts | Last post October 28, 2014
    • Hi, Tom !
      The link to the tutorial "EF 6 MVC 5 VS 2013 tutorial series" is broken, where can I see the tutorial?
      (http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application).
      
      Excelent tutorial, I'm doing it but kept on part 2
      
      Thanks
      
    • The URL to that tutorial was changed, but redirects have now been created so the URL you cite now works again.
    • Why in my IE11 browser data picker is not working for Enrollment Date when I am trying to create a New Student.
  • 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
      
      http://lambangdaihoc.net/lam-bang-dai-hoc-tai-tphcm/
  • 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?
      
      Thanks
      
    • 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).
11 - 20 of 57 Items