零框架多租户设计方案
This commit is contained in:
1 parent
b7caaa2d0c
commit
b4a704526d
42 files changed
+1002
-1
No files matched your search
+27
@@ -0,0 +1,27 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.SchemaIsolationService.Entities;
|
||||
using MultiTenancySample.SchemaIsolationService.EntityFrameworks;
|
||||
|
||||
namespace MultiTenancySample.SchemaIsolationService.Controllers
|
||||
{
|
||||
[Route("api/{tenant}/[controller]")]
|
||||
[ApiController]
|
||||
public class ProductsController(IDbContextFactory<SchemaIsolationDbContext> dbContextFactory) : ControllerBase
|
||||
{
|
||||
private readonly SchemaIsolationDbContext _dbContext = dbContextFactory.CreateDbContext();
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult GetProducts(string? keyword)
|
||||
{
|
||||
IQueryable<Product> products = _dbContext.Set<Product>();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(keyword))
|
||||
{
|
||||
products = products.Where(p => p.Name.Contains(keyword));
|
||||
}
|
||||
|
||||
return Ok(products);
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
namespace MultiTenancySample.SchemaIsolationService.Entities
|
||||
{
|
||||
public class Product
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public required string Name { get; set; }
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.SchemaIsolationService.Entities;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
||||
namespace MultiTenancySample.SchemaIsolationService.EntityFrameworks
|
||||
{
|
||||
public class DataSeeding(IDbContextFactory<SchemaIsolationDbContext> dbContext, ICurrentTenant currentTenant)
|
||||
{
|
||||
public async Task SeedDataAsync()
|
||||
{
|
||||
currentTenant.SetTenant("Tenant1");
|
||||
|
||||
SchemaIsolationDbContext dbContext1 = await dbContext.CreateDbContextAsync();
|
||||
|
||||
if (await dbContext1.Database.EnsureCreatedAsync())
|
||||
{
|
||||
await dbContext1.AddRangeAsync(new List<Product> {
|
||||
new() { Id = Guid.NewGuid(), Name = "Product1"},
|
||||
new() { Id = Guid.NewGuid(), Name = "Product3"},
|
||||
new() { Id = Guid.NewGuid(), Name = "Product5"}
|
||||
});
|
||||
|
||||
await dbContext1.SaveChangesAsync();
|
||||
}
|
||||
|
||||
currentTenant.SetTenant("Tenant2");
|
||||
|
||||
SchemaIsolationDbContext dbContext2 = dbContext.CreateDbContext();
|
||||
|
||||
await dbContext2.Database.MigrateAsync();
|
||||
|
||||
if (await dbContext2.Database.EnsureCreatedAsync())
|
||||
{
|
||||
await dbContext2.AddRangeAsync(new List<Product>
|
||||
{
|
||||
new() { Id = Guid.NewGuid(), Name = "Product2"},
|
||||
new() { Id = Guid.NewGuid(), Name = "Product4"},
|
||||
new() { Id = Guid.NewGuid(), Name = "Product6"}
|
||||
});
|
||||
|
||||
await dbContext2.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.SchemaIsolationService.Entities;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
||||
namespace MultiTenancySample.SchemaIsolationService.EntityFrameworks
|
||||
{
|
||||
public class SchemaIsolationDbContext(DbContextOptions<SchemaIsolationDbContext> options, ICurrentTenant currentTenant) : DbContext(options)
|
||||
{
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<Product>().Property(p => p.Name).HasMaxLength(32);
|
||||
|
||||
// This scenario is not directly supported by EF Core and is not a recommended solution.
|
||||
// https://learn.microsoft.com/en-us/ef/core/miscellaneous/multitenancy
|
||||
|
||||
modelBuilder.HasDefaultSchema(currentTenant.TenantId);
|
||||
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MultiTenancySample.ServiceDefaults\MultiTenancySample.ServiceDefaults.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Update="appsettings.Development.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</Content>
|
||||
<Content Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,46 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.SchemaIsolationService.EntityFrameworks;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddDbContextFactory<SchemaIsolationDbContext>(options =>
|
||||
{
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString("Default"));
|
||||
}, lifetime: ServiceLifetime.Scoped);
|
||||
|
||||
builder.Services.AddScoped<DataSeeding>();
|
||||
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
builder.Services.AddMultiTenancy();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseMultiTenancy();
|
||||
|
||||
var serviceProvider = app.Services.CreateScope().ServiceProvider;
|
||||
var dataSeeding = serviceProvider.GetRequiredService<DataSeeding>();
|
||||
await dataSeeding.SeedDataAsync();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:40979",
|
||||
"sslPort": 44360
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5258",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7264;http://localhost:5258",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"Default": "Host=localhost;Port=5432;Database=SchemaIsolationService;Username=postgres;Password=postgres"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user