AJBurgess.MultiTenancy 1.0.0

dotnet add package AJBurgess.MultiTenancy --version 1.0.0
NuGet\Install-Package AJBurgess.MultiTenancy -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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
#r "nuget: AJBurgess.MultiTenancy, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AJBurgess.MultiTenancy as a Cake Addin
#addin nuget:?package=AJBurgess.MultiTenancy&version=1.0.0

// Install AJBurgess.MultiTenancy as a Cake Tool
#tool nuget:?package=AJBurgess.MultiTenancy&version=1.0.0

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<TId>, 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

Product Compatible and additional computed target framework versions.
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 1,034 8/6/2018