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)
 
 
 
 
 
(148)
440,633 times
Add to favorites
10/22/2015
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • 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..
      Suhani
    • 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 http://www.mikesdotnetting.com/article/241/mvc-5-with-ef-6-in-visual-basic-creating-an-entity-framework-data-model
      
  • The name 'sortOrder' does not exist in the current context
    2 Posts | Last post October 01, 2015
    • 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.
    • Ah never mind I forgot to include the parameter.
  • Seed method do not populate test data in student table in database
    2 Posts | Last post July 29, 2015
    • Hi Tom!
      I've read up to page 33 and implementation of your project. But when run project do not any data in tables! when I add a record in student table in database, I see it on browser. I check any problem in my project but i can not find any it in. When I place break point in seed method and debug my or your project (before I deleted database), re-create database but EF do not call seed method to populate test data in database. Pleas help me. Very Thansk.  
    • If you run the project without executing the update-database command first, EF will run the createifnotexists initializer and the migrations Seed method won't run.  Try changing the database name in Web.config and running update-database.
  • Identity 2.0 Plans?
    2 Posts | Last post July 20, 2015
    • 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 http://asp.net/identity.
  • No dependency injection used?
    3 Posts | Last post July 14, 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
      
      
    • @RajivSKulkarni
      
      You may find this interesting http://mehdi.me/ambient-dbcontext-in-ef6/ It avoids a unit of work. It uses a service layer an dependency injection.
      
  • 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 http://blog.michaelhidalgo.info/2015/06/a-bug-in-contoso-university-sample.html
    • 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' https://www.owasp.org/index.php/Top_10_2010-A4-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 http://bit.ly/1GpaxN4 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:
      
      From:
      connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity2;Integrated Security=SSPI;"
      
      To:
      connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=ContosoUniversity2;Integrated Security=SSPI;"
      
1 - 10 of 64 Items