AJBurgess.MultiTenancy 1.0.0

Easily add multi-tenancy capability to your EF-based .NET applications.

Install-Package AJBurgess.MultiTenancy -Version 1.0.0
dotnet add package AJBurgess.MultiTenancy --version 1.0.0
<PackageReference Include="AJBurgess.MultiTenancy" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AJBurgess.MultiTenancy --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Switching tenants

To change the current tenant, just call ChangeTenant(tenantId) on your DbContext.
This has two effects:

  1. All database queries are automatically filtered to include only data belonging to that tenant
  2. Any new entities added, automatically belong to that tenant
using (MyDbContext context = new MyDbContext())
{
    // Create two tenants
    context.Tenants.Add(new Tenant { Id = "ACL", Name = "Acme Components Ltd" });
    context.Tenants.Add(new Tenant { Id = "RRS", Name = "Road Runner Supplies" });
    context.SaveChanges();

    // Switch to first tenant
    context.ChangeTenant("ACL");

    // Add some account entities, specific to the current tenant
    context.Accounts.Add(new Account { Name = "Dan's Dynamite" });
    context.Accounts.Add(new Account { Name = "Weighty Weights" });
    context.SaveChanges();

    // Switch to second tenant
    context.ChangeTenant("RRS");

    // Add some account entities, specific to the current tenant
    context.Accounts.Add(new Account { Name = "Fake Holes R Us" });
    context.SaveChanges();

    // The data is now stored like this:
    //
    // ACL
    //   |-> Dan's Dynamite
    //   |-> Weighty Weights
    //
    // RRS
    //   |-> Fake Holes R Us

    // Switch back to first tenant
    context.ChangeTenant("ACL");

    // Read the accounts (specific to the current tenant)
    List<Account> accounts = context.Accounts.ToList(); // 2 x accounts only

    // Switch back to second tenant
    context.ChangeTenant("RRS");

    // Read the accounts (specific to the current tenant)
    accounts = context.Accounts.ToList(); // 1 x account only
}

How does it work?

To enable this capability, you need to do several things:

  1. Derive your DbContext class from EntityFramework.MultiTenantDbContext&lt;TId&gt;, where TId is the data type of the primary key of your tenant entity.
  2. In your data model, have an entity that represents your concept of a tenant.
  3. In every entity that is tenant-specific, have two properties: the foreign key ID of the parent tenant, and a navigation property for the tenant entity itself.

For example:

public class MyDbContext : MultiTenantDbContext<string>
{
    public DbSet<Tenant> Tenants { get; set; }
    public DbSet<Account> Accounts { get; set; }
    public DbSet<Transaction> Transactions { get; set; }

    public MyDbContext() : base("MultiTenancy.SampleApp")
    {
    }
}

public class Tenant
{
    [Key]
    [MaxLength(10)]
    [Required(AllowEmptyStrings = false)]
    public string Id { get; set; }

    [MaxLength(50)]
    public string Name { get; set; }
}

public class Account
{
    [Key]
    public int Id { get; private set; }

    [Required]
    public string TenantId { get; private set; }

    public virtual Tenant Tenant { get; set; }

    [MaxLength(50)]
    public string Name { get; set; }

    public virtual ICollection<Transaction> Transactions { get; set; }
}

public class Transaction
{
    [Key]
    public int Id { get; private set; }

    public int AccountId { get; set; }

    [Required]
    public virtual Account Account { get; set; }

    [Required]
    public string TenantId { get; private set; }

    public virtual Tenant Tenant { get; set; }

    [MaxLength(50)]
    public string Description { get; set; }
}

Further information

For further information, see the project wiki

Switching tenants

To change the current tenant, just call ChangeTenant(tenantId) on your DbContext.
This has two effects:

  1. All database queries are automatically filtered to include only data belonging to that tenant
  2. Any new entities added, automatically belong to that tenant
using (MyDbContext context = new MyDbContext())
{
    // Create two tenants
    context.Tenants.Add(new Tenant { Id = "ACL", Name = "Acme Components Ltd" });
    context.Tenants.Add(new Tenant { Id = "RRS", Name = "Road Runner Supplies" });
    context.SaveChanges();

    // Switch to first tenant
    context.ChangeTenant("ACL");

    // Add some account entities, specific to the current tenant
    context.Accounts.Add(new Account { Name = "Dan's Dynamite" });
    context.Accounts.Add(new Account { Name = "Weighty Weights" });
    context.SaveChanges();

    // Switch to second tenant
    context.ChangeTenant("RRS");

    // Add some account entities, specific to the current tenant
    context.Accounts.Add(new Account { Name = "Fake Holes R Us" });
    context.SaveChanges();

    // The data is now stored like this:
    //
    // ACL
    //   |-> Dan's Dynamite
    //   |-> Weighty Weights
    //
    // RRS
    //   |-> Fake Holes R Us

    // Switch back to first tenant
    context.ChangeTenant("ACL");

    // Read the accounts (specific to the current tenant)
    List<Account> accounts = context.Accounts.ToList(); // 2 x accounts only

    // Switch back to second tenant
    context.ChangeTenant("RRS");

    // Read the accounts (specific to the current tenant)
    accounts = context.Accounts.ToList(); // 1 x account only
}

How does it work?

To enable this capability, you need to do several things:

  1. Derive your DbContext class from EntityFramework.MultiTenantDbContext&lt;TId&gt;, where TId is the data type of the primary key of your tenant entity.
  2. In your data model, have an entity that represents your concept of a tenant.
  3. In every entity that is tenant-specific, have two properties: the foreign key ID of the parent tenant, and a navigation property for the tenant entity itself.

For example:

public class MyDbContext : MultiTenantDbContext<string>
{
    public DbSet<Tenant> Tenants { get; set; }
    public DbSet<Account> Accounts { get; set; }
    public DbSet<Transaction> Transactions { get; set; }

    public MyDbContext() : base("MultiTenancy.SampleApp")
    {
    }
}

public class Tenant
{
    [Key]
    [MaxLength(10)]
    [Required(AllowEmptyStrings = false)]
    public string Id { get; set; }

    [MaxLength(50)]
    public string Name { get; set; }
}

public class Account
{
    [Key]
    public int Id { get; private set; }

    [Required]
    public string TenantId { get; private set; }

    public virtual Tenant Tenant { get; set; }

    [MaxLength(50)]
    public string Name { get; set; }

    public virtual ICollection<Transaction> Transactions { get; set; }
}

public class Transaction
{
    [Key]
    public int Id { get; private set; }

    public int AccountId { get; set; }

    [Required]
    public virtual Account Account { get; set; }

    [Required]
    public string TenantId { get; private set; }

    public virtual Tenant Tenant { get; set; }

    [MaxLength(50)]
    public string Description { get; set; }
}

Further information

For further information, see the project wiki

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 262 8/6/2018