CTEC2402: Sofware Development Project

Code First: Model Classes

In MVC there are three approaches to developing the database. In code first simple C# classes are created, each one matching a Database table. Thus a Customers database table would be matched by a Customer class. Note table names are plural and class names are singular. This naming convention must be strictly followed if the automatic linking of classes to tables is to occur without errors.

Once the classes have been created, they can be used to automatically create a corresponding database. However any changes to the classes will result in the datbase being re-created. This deletes all existing data in the database. So please do not put any real data in the database until all the classes have been created and annotated.

  • Tutorial: Getting Started in ASP.NET MVC5. This tutorial covers creating a simple Movie class. It also covers the database context class and web configuration. A context class can be created automatically for you as I will demonstrate in class, and the configuration is covered in a later web page.
  • Tutorial: Code First Using EF6 in MVC5 This is a more detailed look at the code first approach of generating the database from the model classes. It is based on Contosso University example.
  • Tutorial: Music Store - Models and Data Access. Only the first part is relevant. It is a better example than the MVC4 tutorial as it includes several classes with relationships between them.
  • Video: MVC5 Entity Framework 6 The most relevant videos are the first five. It might be easier to find from the new front page to MVC. Just click Video 6 in the Pluralsight list of essential videos.