OswaldTechnologies.Multitenancy 2.0.1

dotnet add package OswaldTechnologies.Multitenancy --version 2.0.1
NuGet\Install-Package OswaldTechnologies.Multitenancy -Version 2.0.1
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="OswaldTechnologies.Multitenancy" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OswaldTechnologies.Multitenancy --version 2.0.1
#r "nuget: OswaldTechnologies.Multitenancy, 2.0.1"
#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 OswaldTechnologies.Multitenancy as a Cake Addin
#addin nuget:?package=OswaldTechnologies.Multitenancy&version=2.0.1

// Install OswaldTechnologies.Multitenancy as a Cake Tool
#tool nuget:?package=OswaldTechnologies.Multitenancy&version=2.0.1

ASP.NET Core Multitenancy

Nuget Nuget

This library lets you add multitenancy to an ASP.Core application. You add its services to the DI container. It adds a ITenantService that provides two methods. GetCurrentTenant fetches the current tenant configuration based on the current hostname. GetTenantList returns a list of all tenants in configuration. You are able to use this service in other classes to change your database connection string.

How to use

Install the library.

Add the tenant configuration to appsettings.json.

{
    "Tenants": [
        {
            "Name": "Localhost Tenant",
            "Hostname": "localhost:44331",
            "ConnectionString": "conn1"
        },
        {
            "Name": "Test Tenant",
            "Hostname": "test.example.com",
            "ConnectionString": "conn2"
        }
    ]
}

Add the Multitenancy services to the dependency injection container.

using Multitenancy;

public void ConfigureServices(IServiceCollection services)
{
    services.AddMultitenancy();
}

Inject into DbContext to specify the database connection string for the current tenant.

public class ExampleContext : DbContext
{
    private readonly ITenantService _tenantService;

    public ExampleContext(DbContextOptions<ExampleContext> options, ITenantService tenantService)
        : base(options)
    {
        _tenantService = tenantService;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);

        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(_tenantService.GetCurrentTenant().ConnectionString);
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

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
2.0.1 329 11/11/2021
2.0.0 333 11/10/2021
1.0.0 337 11/4/2021
0.9.0 330 4/8/2023