2.0: Add User Entity and DbContext

Back in our .API project, let's add a User entity to represent our users.

We'll start out with a basic class and get authentication working before expanding it.

In the EFConnect.Data project, add a new folder called Entities.

Inside of the Entities folder - add a new class called User.cs. With the C# extension installed - right click on the folder and select New C# Class. Otherwise, you will have to write out the boilerplate.

namespace EFConnect.Data.Entities
{
    public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public byte[] PasswordHash { get; set; }
        public byte[] PasswordSalt { get; set; }
    }
}

Notice that the PasswordHash and PasswordSalt properties are byte arrays. These will be used for securely encrypting our User's passwords. It will be explained later - but, make sure that they are byte arrays for now.

Add DbContext

We need to add a package reference for Entity Framework Core and SQL Server in the Data project.

In your terminal, while inside of the EFConnect.Data project enter these three commands:

dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet restore

Inside of the EFConnect.Data project - add a new class called EFConnectContext.cs.

using EFConnect.Data.Entities;
using Microsoft.EntityFrameworkCore;

namespace EFConnect.Data
{
    public class EFConnectContext : DbContext
    {
        public EFConnectContext(DbContextOptions<EFConnectContext> options) : base(options) {}

        public DbSet<User> Users { get; set; }
    }
}

Add Connection String

In your .API project - inside of the appsettings.json file - add a section called "ConnectionStrings" and with the following connection string:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=EFConnect;Trusted_Connection=True;Integrated Security=SSPI"
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

Registering The DbContext in Startup.cs

Now we have our first opportunity to register a class for dependency injection in our service container.

In Startup.cs, inside of the ConfigureServices() method add the following (and make sure it's above services.AddMvc()).

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<EFConnectContext>(x => x.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    services.AddMvc();
}

The connection string could be entered as a string literal inside of the UseSqlServer() method. But, it is better to store the connection string inside of a configuration file.

The Configuration is added to our Startup class inside of the constructor. It has a method for accessing different sections of our appsettings.json - in this case, we're accessing the "ConnectionStrings" section with the GetConnectionString() method.

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

Last updated