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# (913.1 KB)
 
 
 
 
 
(102)
264,501 times
Add to favorites
4/16/2014
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • MVC ModelBinding Vulnerability
    3 Posts | Last post November 13, 2013
    • Hi Tom, as described in "MVC ModelBinding Vulnerability in Contoso University sample (first raw PoC)" http://blog.diniscruz.com/2013/07/mvc-modelbinding-vulnerability-in.html there are a number of MVC ModelBinding Vulnerabilities in this application.
      
      I would like to use this application has an example of how to fix these issues, so have you added this code to GitHub? (so that I can fork it and make my code changes there?)
    • We've updated the application both here in Code Gallery and on the archive.msdn site where the chapter downloads are stored. I'm not aware that there is a copy of this project on GitHub -- if someone has put one there I don't have control over it.  The download project shows one method to address the vulnerability - using the Bind attribute. Another method is to use view models and not bind directly to entity classes. The options are discussed in a new security note sidebar in tutorial #2.
    • I believe it is better to rely on the available links in Gallery MSDN
  • Database connection
    2 Posts | Last post October 01, 2013
    • I like your app so much and your work is highly appreciated. I just have two questions for you:
      
      In Department controller:
      
      1-private SchoolContext db = new SchoolContext();
      
      how the database connection(from web.config) is passed to schoolcontext instance 
      
      2-in index action, you use db.department
      Where is department initialized?
      
      Thank you in advance
    • 1. EF looks for a connection string with the same name as the context class. See the first tutorial in the series for more information about this.  
      2. Departments DbSet property is defined in the context class. See the first and fourth tutorials in the series for more information about this.
  • Gayan Kasun
    2 Posts | Last post Fri 2:40 PM
    • Hi Sir I downloaded solution file and restore it using package manager console.After that i ran the update-database command.but i got an following error --'Cannot find the object "dbo.Student" because it does not exist or you do not have permissions.'''
      
      Please givethe answer
    • Make sure you have the right software installed.  See the Prerequisites section of the first tutorial in the series: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
      Also make sure that you follow the instructions for downloading and running the application. See the Downloading and Building page at: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/building-the-ef5-mvc4-chapter-downloads
  • where data insert and fetch ??
    5 Posts | Last post August 06, 2013
    • Hi Sir,
      
      i downloaded source code and learn how it works
      i insert a student its successfully insert and also populate in list
      but i am not bale to see data coming from and insert
      please help me.
    • What do you mean by "not able to see data coming from and insert", can you explain a little more about where you're looking for data and not seeing it?
    • i added one student but that student are not showing in DAL/SchollInitiliazers.cs
      file while all student which is already in the system are there but whose i added it's not showing in this file so i have my Q where does he go 
      please help me 
    • thanks for reply 
      i am getting the answer of my Q i read your code documentation
      sir can you help me how to learn mvc with entity framework
      i mean what will be the best way to learn and implementation 
    • See the tutorial that goes with the download:  http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
      
      When you add students using the UI they won't go into the seed method code, they'll go into the database; see the tutorial for more information about the seed method and what it's for.
  • connection string typo
    2 Posts | Last post June 30, 2013
    • Hi- You have a typo in config file: \C#\ContosoUniversity\Web.config
        
      It should have a backslash in front of the data file name, like this:
      connectionString="Data Source=|DataDirectory|\School.sdf" 
       
      Thanks for creating this-
      
    • Thanks for reporting that, but since both formats work, why do you consider omitting the backslash to be a typo?
  • Changin target platform to x64
    2 Posts | Last post June 30, 2013
    • Hi, I have some 3rd party Dlls(x64) which i am trying to use with this application, if i change the target platform of thsi application to x64, it fails with the following exception 
      
      Source Error: 
      
      An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
      
      Stack Trace: 
      
      
      [BadImageFormatException: Could not load file or assembly 'ContosoUniversity' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
         System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
         System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +39
         System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks) +132
         System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +144
         System.Reflection.Assembly.Load(String assemblyString) +28
         System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +46
      
       
      are these any native x86 dlls refered in thge project, is there any way i can use this project tagetting x64 platform 
    • I don't know the answer to that; I suggest posting a question to a forum or StackOverflow.com.
  • Tutorial section 3 of 10 - Adding a Search Box to the Students Index Page
    2 Posts | Last post June 15, 2013
    • Hi, I'm unable to get the Search feature to work.  I get the error below.  Any suggestions?
      
      Server Error in '/' Application.
      
      The resource cannot be found.
      
      Description: 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.  Please review the following URL and make sure that it is spelled correctly. 
      
      Requested URL: /Student
      
      Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18044
    • Does this happen while working through the tutorial, or when you download and run the completed project?  If you're going through the tutorial, you can download the completed project and compare the code to what you built manually to see if you missed a step somewhere.
  • where do i look for database relations...so i can delete.
    2 Posts | Last post May 07, 2013
    • The primary key value cannot be deleted because references to this key still exist. [ Foreign key constraint name = Department_Administrator ]
      
      Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
      
      Exception Details: System.Data.SqlServerCe.SqlCeException: The primary key value cannot be deleted because references to this key still exist. [ Foreign key constraint name = Department_Administrator ]
      
      Source Error: 
      
      
      Line 198:            Instructor instructor = db.Instructors.Find(id);
      Line 199:            db.Instructors.Remove(instructor);
      Line 200:            db.SaveChanges();
      Line 201:            return RedirectToAction("Index");
      Line 202:        }
    • Since the constraint is Department_Administrator, I would look in the Department table, PersonID column, for the instructor ID of the instructor you want to delete, then change Department.PersonID to null before you delete the instructor. 
  • How to handle concurrency
    5 Posts | Last post March 20, 2013
    • Hi Tom! I create a application based on ContosoUniversity.
      I made some modifications to meet the requirements of the system as the code presented in stackoverflow: http://stackoverflow.com/questions/15390365/concurrency-mvc-4-with-timestamp-error
      The problem is that the time to validate the concurrency bytes are loaded with different values ​​(in the browser A, the bytes displayed are 0,0,0,0,0,0,8,7 and B Browser the bytes displayed are 0 , 0,0,0,0,0,23,113) so the concurrency is not treated because the values ​​are different. You can see in my code what am I doing wrong?
      Thks for support!
    • Hello hugoterra, For questions that aren't directly about the Contoso U app or tutorial itself, you're more likely to get answers from StackOverflow or a forum thread. If your SO post doesn't get an answer you might try the EF forums at http://forums.asp.net/1227.aspx and http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/threads/
    • Ok Tom! Thks for the help! :)
    • Very Good, Thanks...
    • Thanks a lot!
  • Paging a viewModel
    1 Posts | Last post February 10, 2013
    • If you need to page a ViewModel, put the PageList.IPageList<your class> in the ViewModel. For example:
      
      namespace MyProject.ViewModels
      {
          public class IndexData 
          {
              public PagedList.IPagedList<Entity1> AEntities { get; set; }
              public IEnumerable<Entity2> BEntities { get; set; }
              public List<SelectListItem> TheType { get; set; }
              public Entity1 SelectedEntity { get; set; }
          }
      }
      
      and in the .cshtml file, put:
      @model MyProject.ViewModels.IndexData
      
      I didn't see this explained in the tutorial. Hope it saves you some time.
11 - 20 of 42 Items