Loading [Contrib]/a11y/accessibility-menu.js

Multi-tenant ASP.NET Core 1 - Implementing tenant provider


Implementing tenant provider

Multi-tenant web applications need the way how to find out the current tenant. This example shows how to implement and apply simple tenant provider to filter out current tenant enitites in database context.

As there can be multiple implementations of tenant providers we start with defining interface for tenant providers.

public interface ITenantProvider
    Guid GetTenantId();

For demo purposes we need also some example entity. Let it be Person entity with tenant ID field.

public class Person
    public Guid Id { get; set; }
    public Guid TenantId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

We also need database context that uses tenant provider. Here is sample context that keeps known tenant ID-a for demo purposes. Notice how tenant provider is injected to context through constructor and how Entity Framework Core 2 global query filters are used to filter out people by current tenant.

public class MultitenantDbContext : DbContext
    public static Guid Tenant1Id = Guid.Parse("51aab199-1482-4f0d-8ff1-5ca0e7bc525a");
    public static Guid Tenant2Id = Guid.Parse("ae4e21fa-57cb-4733-b971-fdd14c4c667e");

    public DbSet<Person> People { get; set; }

    private ITenantProvider _tenantProvider;

    public MultitenantDbContext(DbContextOptions<MultitenantDbContext> options,
                                ITenantProvider tenantProvider) : base(options)
        _tenantProvider = tenantProvider;

    protected override void OnModelCreating(ModelBuilder modelBuilder)

        modelBuilder.Entity<Person>().HasQueryFilter(p => p.TenantId == _tenantProvider.GetTenantId());

    public void AddSampleData()
        People.Add(new Person {
            Id = Guid.Parse("79865406-e01b-422f-bd09-92e116a0664a"),
            TenantId = Tenant1Id,
            FirstName = "Gunnar",
            LastName = "Peipman"

        People.Add(new Person
            Id = Guid.Parse("d5674750-7f6b-43b9-b91b-d27b7ac13572"),
            TenantId = Tenant2Id,
            FirstName = "John",
            LastName = "Doe"

        People.Add(new Person
            Id = Guid.Parse("e41446f9-c779-4ff6-b3e5-752a3dad97bb"),
            TenantId = Tenant1Id,
            FirstName = "Mary",
            LastName = "Jones"


Before going to demo we need also implementation for tenant provider. Here I give dummy tenant provider that always returns same tenant ID.

public class DummyTenantProvider : ITenantProvider
    public Guid GetTenantId()
        return MultitenantDbContext.Tenant1Id;


Click to run the .NET Core Web app.
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using MultitenantAspNetCore.Models;
namespace MultitenantAspNetCore.Controllers
public class HomeController : Controller
private readonly MultitenantDbContext _context;


Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
Online Participants