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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.