title: How to implement database per tenant strategy on ASP.NET Core
body: <p>If every tenant has its own database and problem here is how to decide dynamically which connection string needed to be injected and how to make connection strings available.</p>
  
  <p>I have found an article on "Implementing database per tenant strategy on ASP.NET Core" and this explain everything clearly. 
  <a href="https://gunnarpeipman.com/aspnet-core-database-per-tenant/" rel="nofollow noreferrer">https://gunnarpeipman.com/aspnet-core-database-per-tenant/</a></p>
  
  <p>First define new Tenant class</p>
  
  <pre><code>public class Tenant
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public string Host { get; set; }
      public string DatabaseConnectionString { get; set; }
  }
  </code></pre>
  
  <p>And we need a separate MasterDb to store tenant configurations. And that means it requires separate DbContext to represent a session with the MasterDb. </p>
  
  <pre><code>public class MasterDbContext : DbContext
  {
      private DbSet&lt;Tenant&gt; Tenants { get; set; }
  
      public MasterDbContext(DbContextOptions&lt;MasterDbContext&gt; options) : base(options){}
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Tenant&gt;().HasKey(e =&gt; e.Id);
      }
  
      public Tenant GetTenant(IHttpContextAccessor accessor)
      {
          Tenant tenant = Tenants.FirstOrDefault(t =&gt; t.Host.ToLower() == accessor.HttpContext.Request.Host.Value.ToLower());
          return tenant;
      }
  }
  </code></pre>
  
  <p>Now it’s time to bring ITenantProvider and write an implementation that uses MasterDbContext. This provider holds all the logic about detecting host header and getting current tenant.</p>
  
  <pre><code>public interface ITenantProvider
  {
     Tenant GetTenant();
  }
  
  public class TenantProvider : ITenantProvider
  {
      private readonly Tenant _tenant;
  
      public TenantProvider(IHttpContextAccessor accessor,
                                  MasterDbContext context)
      {
          _tenant = context.GetTenant(accessor);
      }
  
      public Tenant GetTenant()
      {
          return _tenant;
      }
  }
  
  </code></pre>
  
  <p>Configuring OrganizationDbContext dynamically.</p>
  
  <pre><code>public class OrganizationDbContext : DbContext
  {
      private readonly Tenant _tenant;
  
      public DbSet&lt;Branch&gt; Branches { get; set; }
      public DbSet&lt;Employee&gt; Employees { get; set; }
  
      public OrganizationContext(DbContextOptions&lt;OrganizationContext&gt; options,ITenantProvider tenantProvider): base(options)
      {
          _tenant = tenantProvider.GetTenant();
      }
  
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
          optionsBuilder.UseNpgsql(_tenant.DatabaseConnectionString);
          base.OnConfiguring(optionsBuilder);
      }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Branch&gt;().HasKey(e =&gt; e.Id);
          modelBuilder.Entity&lt;Employee&gt;().HasKey(e =&gt; e.Id);
  
          base.OnModelCreating(modelBuilder);
      }
  }
  </code></pre>