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)
221,015 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • Gayan Kasun
    2 Posts | Last post August 30, 2013
    • 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:
      Also make sure that you follow the instructions for downloading and running the application. See the Downloading and Building page at:
  • 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:
      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
  • 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 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:
      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 and
    • 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.
  • how can i solve this problem?
    2 Posts | Last post January 19, 2013
      Unable to load the native components of SQL Server Compact corresponding to the ADO.NET provider of version 8876. Install the correct version of SQL Server Compact. Refer to KB article 974247 for more details.
      but i have allready install the SQLSERVER COMPACT EDDITION ..........i am still getting this error
    • Make sure you've installed SQL Server Compact 4.0, not 3.5, and you might try installing the SqlServerCompact NuGet package.  Did you check the KB article mentioned in the error message?  It has some suggestions also.
  • UnitOfWork only in CourseController
    3 Posts | Last post December 08, 2012
    • 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?
    • 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!
11 - 20 of 41 Items