Creating the database using Code-First
Before going further, let's do a quick checklist:
- Are we done with our entities? Yes
- Do we have a DBMS and a
WorldCities
database available? Yes - Have we gone through all of the required steps we need to complete to actually create and fill in the aforementioned database using Code-First? No
As a matter of fact, we need to take care of two more things:
- Set up an appropriate Database Context
- Enable Code-First Data Migrations support within our project.
Within the following sections, we're going to fill all of these gaps and eventually fill our WorldCities
database.
Setting up the DbContext
To interact with data as objects/entity classes, Entity Framework Core uses the Microsoft.EntityFrameworkCore.DbContext
class, also called DbContext
or simply Context. This class is in charge of all of the entity objects during runtime, including populating them with data from the database...