Skip to content

Search EricksonLopez.* Ecosystem

Type a library name, NuGet package ID, .NET type, or architectural concept to search across the portal.

Getting Started with EricksonLopez.MultiTenancy

Install EricksonLopez.MultiTenancy (2.0.0 on NuGet) together with AspNetCore, PostgreSql, and Dapper packages.

dotnet add package EricksonLopez.MultiTenancy --version 2.0.0
Terminal window
dotnet add package EricksonLopez.MultiTenancy.AspNetCore --version 2.0.0
dotnet add package EricksonLopez.MultiTenancy.PostgreSql --version 2.0.0
dotnet add package EricksonLopez.MultiTenancy.Dapper --version 2.0.0

TenantId, TenantInfo, and AspNetCoreMultiTenancyExtensions all reside in namespace EricksonLopez.MultiTenancy;. Note that InMemoryTenantStore<T> registers tenants via AddOrUpdate(...):

using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using Dapper;
using EricksonLopez.MultiTenancy;
using EricksonLopez.MultiTenancy.Dapper;
using EricksonLopez.MultiTenancy.PostgreSql;
using Microsoft.Extensions.DependencyInjection;
namespace Example.MultiTenancyQuickStart;
public static class MultiTenancySetup
{
public static void ConfigureServices(IServiceCollection services)
{
var acmeId = TenantId.Create("11111111-1111-1111-1111-111111111111");
services.AddAspNetCoreMultiTenancy(opt => opt.RequireTenant = true);
services.AddInMemoryTenantStore<TenantInfo>(store =>
store.AddOrUpdate(new TenantInfo(acmeId, "acme-corp", isActive: true)));
services.AddHostNameTenantStrategy();
}
public static async Task<int> QueryTenantInvoicesAsync(
DbConnection connection,
ITenantContext tenantContext,
CancellationToken ct)
{
await using DbTransaction tx = await connection.BeginTenantTransactionAsync(
tenantContext,
cancellationToken: ct);
DynamicParameters parameters = tenantContext.CreateTenantParameters();
int count = await connection.ExecuteScalarAsync<int>(
"SELECT COUNT(*) FROM invoices WHERE tenant_id = @TenantId",
parameters,
transaction: tx);
await tx.CommitAsync(ct);
return count;
}
}