Skip to Content
Learn
Data Models
Build the Entity Framework Context

To support database creation and maintenance, we’ll need to add some code to our site.

The Data folder will store the database-specific code needed for Microsoft Entity Framework (EF). This includes the context definition and sample data, both of which are C# classes.

Using its included code generators, EF can generate the context class for you but we will instead build this from scratch. Later in the lesson we will add sample data by building another class file and calling it each time the site is run.

The context is a class that acts as a middle man. It has C# methods that you can call to ask the database to do something useful like add a record or return all rows in a table. It relies on the connection string in appsettings.json to physically locate the database file.

This middle man is often referred to as a service. A web site might have many services that perform different functions for any number of pages. ASP.NET uses a technique called dependency injection to expose these middle-men services to each page as needed.

Here are the steps needed to build the context and implement it as an injectable service:

  • Add a context definition which is a C# class
  • Define two DbSet List objects, each representing a database table
  • Include the OnModelCreating() method that creates tables in the database if they are needed
  • Edit the Startup.cs file to configure the context as a service
  • Edit the Program.cs to conditionally create the database as needed

Instructions

1.

The file Data/CountryContext.cs is open. Add two using statements at the top of the file:

  • One to enable EF (Microsoft.EntityFrameworkCore) and
  • One that points to the model classes created in the previous exercise (RazorCountry.Models).
2.

Add a namespace statement for RazorCountry.Data. This goes under the using statements.

3.

Define a public class named CountryContext and have it inherit from DbContext. This is the base Entity Framework class that defines how a context can work. This goes inside the namespace.

4.

Add a class constructor that initializes the base options. A constructor is a public method with the same name as the class. It takes a parameter DbContextOptions of type <CountryContext> which we will refer to as options.

Finally, call the base class and pass in the options. Use the : base (args) syntax.

5.

Create two public DbSet properties that act as gateways to the two tables:

  • The first will be named Continents, of type DbSet<Continent>
  • The second will be name Countries, of type DbSet<Country>

Don’t forget the getters and setters. This goes inside the class definition, not the constructor.

6.

Add the protected override OnModelCreating() method. This code actually creates the database tables if they do not yet exist. It should:

  • Return nothing
  • Accept a parameter modelBuilder of type ModelBuilder
  • In the method body, for each table, call the modelBuilder.Entity() method. Give it a type parameter matching the entity name and then call ToTable() passing in the desired table name as a string.

Here’s an example with an imaginary City table:

modelBuilder.Entity<City>().ToTable("City");
Folder Icon

Sign up to start coding

Already have an account?