OnlineMenu.MultiTenancy.EntityFrameworkCore
1.0.0
dotnet add package OnlineMenu.MultiTenancy.EntityFrameworkCore --version 1.0.0
NuGet\Install-Package OnlineMenu.MultiTenancy.EntityFrameworkCore -Version 1.0.0
<PackageReference Include="OnlineMenu.MultiTenancy.EntityFrameworkCore" Version="1.0.0" />
<PackageVersion Include="OnlineMenu.MultiTenancy.EntityFrameworkCore" Version="1.0.0" />
<PackageReference Include="OnlineMenu.MultiTenancy.EntityFrameworkCore" />
paket add OnlineMenu.MultiTenancy.EntityFrameworkCore --version 1.0.0
#r "nuget: OnlineMenu.MultiTenancy.EntityFrameworkCore, 1.0.0"
#:package OnlineMenu.MultiTenancy.EntityFrameworkCore@1.0.0
#addin nuget:?package=OnlineMenu.MultiTenancy.EntityFrameworkCore&version=1.0.0
#tool nuget:?package=OnlineMenu.MultiTenancy.EntityFrameworkCore&version=1.0.0
OnlineMenu.Foundation
Foundation packages for building multi-tenant SaaS applications with .NET and Entity Framework Core. These packages provide the essential building blocks for Domain-Driven Design, security, and multi-tenancy.
๐ฆ Packages
๐ Quick Start
Installation
# Install all packages
dotnet add package OnlineMenu.DomainCore
dotnet add package OnlineMenu.Security
dotnet add package OnlineMenu.MultiTenancy.EntityFrameworkCore
Basic Usage
1. Domain Entities
using OnlineMenu.DomainCore.Entities;
// Simple entity
public class Product : BaseEntity
{
public string Name { get; set; }
public decimal Price { get; set; }
}
// Multi-tenant entity
public class Order : BaseTenantEntity
{
public Guid ProductId { get; set; }
public int Quantity { get; set; }
public decimal TotalPrice { get; set; }
}
2. Security Claims
using OnlineMenu.Security.Claims;
public class MyController : ControllerBase
{
[Authorize]
public IActionResult GetUserInfo()
{
var tenantId = User.GetTenantId();
var email = User.GetEmail();
var isSuperUser = User.IsSuperUser();
return Ok(new { tenantId, email, isSuperUser });
}
}
3. Multi-Tenancy Setup
using OnlineMenu.MultiTenancy.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Register multi-tenancy services
builder.Services.AddMultiTenancy();
// Configure your DbContext to filter by tenant
builder.Services.AddDbContext<MyDbContext>(options =>
{
options.UseNpgsql(connectionString);
});
๐ Documentation
OnlineMenu.DomainCore
Provides base classes for Domain-Driven Design:
- BaseEntity: Base class with
Id,ExternalId, and audit fields - BaseTenantEntity: Base class for tenant-scoped entities
- IAggregateRoot: Marker interface for aggregate roots
- HasDomainEventsBase: Base class for entities that raise domain events
- DomainEventBase: Base class for domain events
Example:
public class TodoItem : BaseTenantEntity
{
public string Title { get; private set; }
public bool IsCompleted { get; private set; }
public TodoItem(string title, Guid tenantId, Guid userId)
{
Title = title;
SetTenant(tenantId);
SetUser(userId);
}
public void MarkComplete()
{
IsCompleted = true;
UpdateTimestamp();
}
}
OnlineMenu.Security
Provides security-related utilities:
- OnlineMenuClaimTypes: Standard claim type constants
- OnlineMenuRoles: Standard role constants
- ClaimsPrincipalExtensions: Extension methods for
ClaimsPrincipal
Available Extension Methods:
// Identity claims
Guid? tenantId = user.GetTenantId();
string? userId = user.GetSubjectId();
string? email = user.GetEmail();
string? name = user.GetName();
// Role checks
bool isSuperUser = user.IsSuperUser();
bool isAdmin = user.IsAdmin();
bool canManageUsers = user.CanManageUsers();
bool canViewUsers = user.CanViewUsers();
Standard Roles:
SuperUser- Full system accessAdmin- Tenant administratorManageUsers- Can manage usersViewUsers- Can view users
OnlineMenu.MultiTenancy.EntityFrameworkCore
Provides multi-tenancy support for EF Core:
- Tenant: Core tenant entity with auth configuration
- TenantStatus: Enum for tenant states (Enabled/Disabled)
- ICurrentTenantService: Service to access current tenant context
- CurrentTenantService: Implementation that extracts tenant from HTTP context
Setup:
// Program.cs
builder.Services.AddMultiTenancy();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => {
options.Authority = "https://your-identity-provider.com";
});
// DbContext.cs
public class MyDbContext : DbContext
{
private readonly ICurrentTenantService _currentTenantService;
public MyDbContext(
DbContextOptions<MyDbContext> options,
ICurrentTenantService currentTenantService) : base(options)
{
_currentTenantService = currentTenantService;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Apply global tenant filter
modelBuilder.Entity<Order>().HasQueryFilter(e =>
_currentTenantService.TenantId == null ||
e.TenantId == _currentTenantService.TenantId);
}
}
Tenant Entity Features:
var tenant = new Tenant("Acme Corp", TenantStatus.Enabled);
// Update basic info
tenant.Update("Acme Corporation", TenantStatus.Enabled,
logoUrl: "https://example.com/logo.png",
primaryColor: "#FF5733");
// Configure authentication
tenant.UpdateAuthConfiguration(
primaryAuthMethod: 1, // Phone OTP
allowPhoneAuth: true,
allowEmailAuth: true,
otpCodeLength: 6,
otpExpiryMinutes: 5,
smsProvider: "Twilio",
requireSmsVerification: true
);
๐๏ธ Architecture
These packages follow these principles:
- Domain-Driven Design: Clear separation between domain, infrastructure, and application layers
- Dependency Inversion: Depend on abstractions, not concrete implementations
- Single Responsibility: Each package has a focused purpose
- Multi-Tenancy First: Built-in support for SaaS applications
- Security First: JWT-based authentication with role-based authorization
๐ง Advanced Usage
Domain Events
public class OrderPlacedEvent : DomainEventBase
{
public Guid OrderId { get; }
public decimal TotalAmount { get; }
public OrderPlacedEvent(Guid orderId, decimal totalAmount)
{
OrderId = orderId;
TotalAmount = totalAmount;
}
}
public class Order : BaseTenantEntity
{
public void Place()
{
// Business logic...
RegisterDomainEvent(new OrderPlacedEvent(ExternalId, TotalPrice));
}
}
Custom Authorization
[Authorize]
public class AdminController : ControllerBase
{
[HttpGet]
public IActionResult GetDashboard()
{
if (!User.IsAdmin() && !User.IsSuperUser())
return Forbid();
// Admin logic...
return Ok();
}
}
Tenant Isolation
public class OrderService
{
private readonly ICurrentTenantService _tenantService;
private readonly MyDbContext _dbContext;
public async Task<Order> CreateOrderAsync(CreateOrderDto dto)
{
var order = new Order
{
// ... other properties
};
// Automatically set tenant
order.SetTenant(_tenantService.TenantId!.Value);
order.SetUser(_tenantService.UserId!.Value);
_dbContext.Orders.Add(order);
await _dbContext.SaveChangesAsync();
return order;
}
}
๐งช Testing
All packages include comprehensive unit tests:
# Run all tests
dotnet test
# Run with coverage
dotnet test /p:CollectCoverage=true /p:CoverageReportFormat=opencover
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Related Projects
- OnlineMenu.Identity - Authentication and identity management
- OnlineMenuSaaS - Full SaaS application example
๐ฌ Support
- ๐ง Email: support@onlinemenu.com
- ๐ฌ Discussions: GitHub Discussions
- ๐ Issues: GitHub Issues
๐ Acknowledgments
- Built with .NET 8
- Inspired by Domain-Driven Design principles
- Part of the OnlineMenu ecosystem
Made with โค๏ธ by the OnlineMenu team
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Microsoft.Extensions.Http (>= 8.0.1)
- OnlineMenu.DomainCore (>= 1.0.0)
- OnlineMenu.Security (>= 1.0.0)
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 | 74 | 12/28/2025 |