https://github.com/czioutas/multitenancy
Easy-to-use library that adds tenant isolation to ASP.NET Core APIs. Features automatic context handling, built-in middleware, database-level isolation, and Identity integration.
https://github.com/czioutas/multitenancy
csharp dotnet entityframework
Last synced: about 2 months ago
JSON representation
Easy-to-use library that adds tenant isolation to ASP.NET Core APIs. Features automatic context handling, built-in middleware, database-level isolation, and Identity integration.
- Host: GitHub
- URL: https://github.com/czioutas/multitenancy
- Owner: czioutas
- Created: 2025-01-27T18:13:18.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-31T18:23:35.000Z (about 1 year ago)
- Last Synced: 2025-10-12T07:13:34.452Z (6 months ago)
- Topics: csharp, dotnet, entityframework
- Language: C#
- Homepage:
- Size: 78.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Czioutas.Multitenancy
[](https://github.com/czioutas/multitenancy/actions/workflows/publish.yml)
[](https://www.nuget.org/packages/Czioutas.Multitenancy/)
[](https://www.nuget.org/packages/Czioutas.Multitenancy/)
A lightweight, flexible multi-tenancy library for .NET applications that makes it easy to add tenant isolation to your ASP.NET Core APIs using Entity Framework Core.
## Features
- Easy integration with ASP.NET Core and Entity Framework Core
- Automatic tenant context handling through middleware
- Built-in tenant isolation at the database level
- Support for tenant-aware entities
- Flexible tenant identification strategies
- Built-in tenant management API
- Exception handling specific to tenant operations
- Identity integration with custom tenant support
## Installation
Install the package via NuGet:
```bash
dotnet add package Czioutas.Multitenancy
```
## Quick Start
1. Add tenant configuration in your `Program.cs`:
```csharp
builder.Services.AddMultiTenancy(options =>
{
options
.WithDbContext()
.WithUser()
.WithRole()
.WithCurrentUserProvider(sp =>
{
// Your logic to get current user ID
return userId;
})
.WithCurrentUserTenantProvider(sp =>
{
// Your logic to get current tenant ID
return tenantId;
});
});
```
2. Add the middleware in your application pipeline:
```csharp
app.UseMultiTenancy();
```
3. Make your entities tenant-aware by inheriting from `TenantAwareEntity`:
```csharp
public class YourEntity : TenantAwareEntity
{
public string Name { get; set; }
// Your entity properties
}
```
## Configuration Options
### Tenant Identification
You can identify tenants through:
- Custom provider functions defined in your configuration
- HTTP Header "X-Tenant-Id" (used as default and fallback behavior)
### Database Context Setup
Your DbContext should inherit from `TenantIdentityDbContext`:
```csharp
public class YourDbContext : TenantIdentityDbContext
{
public YourDbContext(
DbContextOptions options,
IRequestTenant requestTenant)
: base(options, requestTenant)
{
}
}
```
## Exception Handling
The library includes several specialized exceptions:
- `TenantNotFoundException`
- `TenantAlreadyExistsException`
- `TenantOperationException`
## API Reference
### ITenantService
```csharp
public interface ITenantService
{
Task CreateAsync(string tenantIdentifier);
Task FindByIdentifierAsync(string tenantIdentifier);
Task GetAsync(Guid tenantId);
Task GetAsync(string tenantIdentifier);
string GetRandomIdentifier();
}
```
### IRequestTenant
```csharp
public interface IRequestTenant
{
Guid TenantId { get; }
void SetTenantId(Guid tenantId);
}
```
## Advanced Usage
### Custom Tenant Resolution
```csharp
builder.Services.AddMultiTenancy(options =>
{
options.WithCurrentUserTenantProvider(sp =>
{
var httpContext = sp.GetRequiredService().HttpContext;
var user = httpContext?.User;
// Your custom tenant resolution logic
return tenantId;
});
});
```
### Query Filtering
Tenant isolation is automatically applied to all entities implementing `ITenantAwareEntity`. The library ensures that queries only return data belonging to the current tenant.
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.