So what exactly is a Code First approach? It is exactly what it sounds like. You can model your database objects as entity classes in your code. To establish relationships between those objects, you can define a class to include other classes as member variables. If you already have an existing database, you can create an entity model in your code to represent some (or all) of your database objects.
From our Patient Records example from
Chapter 3
, Understanding MVC, we already have a model class that represents a human. In this chapter, we will add a RobotDoctor
class to our project to build a computerized system for a futuristic hospital with robot doctors. Then, we will establish a relationship between Humans
and RobotDoctors
in the code so that each robot doctor can be assigned to one or more human patients in the database.
In the Models
folder of the sample project from
Chapter 3
, Understanding MVC, let's add...