Defining primary keys and foreign keys via Data Annotations

In the next step, you need to modify the existing Models to be able to persist them within an SQL Database. To allow Entity Framework Core 2.0 to create, read, update and delete records, you need to specify a primary key for each Model. You do that by using Data Annotations, which allow you to decorate a property with the [Key] decorator.

Here is an example of how to use Data Annotations for the UserModel:

    public class UserModel 
    { 
      [Key] 
      public long Id { get; set; } 
      ... 
    }

You should apply this to the UserModel, GameInvitationModel, GameSessionModel and TurnModel of the Tic-Tac-Toe application. You can reuse existing Id properties and decorate them with the [Key] decorator, or add ...

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.