forked from khalidabuhakmeh/EntityFrameworkCoreMultiTenancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
33 lines (26 loc) · 981 Bytes
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using EntityFrameworkCoreMultiTenancy;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScopedAs<TenantService>(new[] {
typeof(ITenantGetter),
typeof(ITenantSetter)
});
builder.Services.AddScoped<MultiTenantServiceMiddleware>();
builder.Services.AddDbContext<Database>(db => {
db.UseSqlite("Data Source=multi-tenant.db");
});
var app = builder.Build();
// initialize the database
using (var scope = app.Services.CreateScope()) {
var db = scope.ServiceProvider.GetRequiredService<Database>();
await db.Database.MigrateAsync();
}
// middleware that reads and sets the tenant
app.UseMiddleware<MultiTenantServiceMiddleware>();
// multi-tenant request, try adding ?tenant=Khalid or ?tenant=Internet (default)
app.MapGet("/", async (Database db) => await db
.Animals
// hide the tenant, which is response noise
.Select(x => new { x.Id, x.Name, x.Kind })
.ToListAsync());
app.Run();