PostgreSQL with EF Part II
Tags: postgres, csharp, dotnet, entityframework
Published: 2021-01-06 17:24:49

Before you read this article, I advise you to read the [first part](https://dev.to/bseyhan/postgresql-with-ef-35fo)

Sometimes some developer needs multiple contexts of our project. This is a little different than a single context. Multiple contexts mean multiple database context. That means is direct to the constructor. In this project, I need authentication and JWT token implementation. So I would like to use Microsoft Identity Server [Document](https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-5.0&tabs=netcore-cli) and I want to store a registered user in a database. Here is the problem I need a new context without CommunityContext. But how? At the same time, I ask myself what is the migration commands for every context. Let me explain.

Create new context and call "AppIdentityDbContext". Every single context include own options like **DbContextOptions<AppIdentityDbContext>**
```
using Core.Entities.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Infrastructure.Identity
{
    public class AppIdentityDbContext : IdentityDbContext<AppUser>
    {
        public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options) :
            base(options)
        {
        }
    }
}
```

AppUser.cs

```
public string DisplayName { get; set; }

public bool IsActive { get; set; } 

public bool IsWantToAuthor { get; set; }

public bool IsAuthor { get; set; }

public UserType UserType { get; set; }
```

Here is the second database context **DbContextOptions<CommunityContext> options**

```
using Core.Entities;
using Core.Entities.Identity;
using Microsoft.EntityFrameworkCore;

namespace Infrastructure
{
    public class CommunityContext : DbContext
    {
        public CommunityContext(DbContextOptions<CommunityContext> options) : base(options)
        {
        }

        public DbSet<Article> Articles { get; set; }

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

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }
}

```
[DbContext](https://docs.microsoft.com/en-us/ef/core/dbcontext-configuration/)

Unlike single context migration commands, multiple contexts command a little complicated. You have to specify the context name.

* Create Migration(example of CommunityContext)
`dotnet ef migrations add Initial -p Infrastructure/ -s API -o Data/Migrations -c CommunityContext`

* Update Migration(example of CommunityContext)
`dotnet ef database update -p Infrastructure/ -s API -c CommunityContext`

![Screen Shot 2021-01-06 at 8.16.26 PM](https://dev-to-uploads.s3.amazonaws.com/i/nvvaar5k2qn198lqnkmo.png)
 
* Create Migration(example of AppIdentityDbContext)
`dotnet ef migrations add IdentityInitial -p Infrastructure -s API -o Identity/Migrations -c AppIdentityDbContext`

* Update Migration(example of AppIdentityDbContext)
`dotnet ef database update -p Infrastructure -s API -c AppIdentityDbContext`

![Screen Shot 2021-01-06 at 8.20.23 PM](https://dev-to-uploads.s3.amazonaws.com/i/isqng21yuwrdz2rat9s1.png)
 
Open the query editor.

![Screen Shot 2021-01-06 at 8.22.19 PM](https://dev-to-uploads.s3.amazonaws.com/i/ubdw0ycdp7vfel127wy4.png)

Thank you 🧑🏻‍💻