Database Configuration

Configuring database context

Install EF Core SQL Server

To install EntityFramework Core SQL Server, go to the package manager. To access the package manager, click on Tools > NuGet Package Manager > Package Manager Console. At the bottom, a console would appear. Type in the following command in the console.

Install-Package Microsoft.EntityFrameworkCore.SqlServer

Create a database context class

Create a folder Data. Inside that folder, create a context class. The name of this class will depend on the application. For example, if you create a contacts application called “People”, your context class will be called PeopleContext.cs.

This class inherits from the DbContext class.

Register model class

The model class that you created needs to be defined here so that Entity Framework can pick it up and map it to a database. To register a model class, type the following line into the context class:

public DbSet<Person> People { get; set; }

If more model classes are created in the future, they too need to be added to the list.

Add constructor

A constructor also needs to be added. Parameter options of type DbContextOptions<PeopleContext> will be passed to it.


The code should look something like this:

Get hands-on with 1200+ tech skills courses.