Establishing a connection

To open a session to the database and query and update instances of your entities, you use a DbContext, which is based on a combination of the unit of work and repository patterns.

Let's see how to prepare the Tic-Tac-Toe application to use Entity Framework Core 2 to connect to an SQL Database by using a DbContext and a connection string:

  1. Go to the Solution Explorer, add a new folder called Data, add a new class called GameDbContext.cs, and implement a DbSet property for each Model (UserModel, TurnModel, and more):
        public class GameDbContext : DbContext 
        { 
          public DbSet<GameInvitationModel> GameInvitationModels { get; set; } public DbSet<GameSessionModel> GameSessionModels { get; set; } public DbSet<TurnModel> TurnModels ...

Get Learning ASP.NET Core 2.0 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.