hello-shop/samples/MultiTenancySample/MultiTenancySample.DatabaseIsolationService/EntityFrameworks/DatabaseIsolationServiceDbContext.cs
2024-07-05 19:25:58 +08:00

33 lines
1.1 KiB
C#

// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
using Microsoft.EntityFrameworkCore;
using MultiTenancySample.DatabaseIsolationService.Entities;
using MultiTenancySample.ServiceDefaults;
namespace MultiTenancySample.DatabaseIsolationService.EntityFrameworks
{
public class DatabaseIsolationServiceDbContext(DbContextOptions<DatabaseIsolationServiceDbContext> options, ICurrentTenant currentTenant, IConfiguration configuration) : DbContext(options)
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().Property(p => p.Name).HasMaxLength(32);
base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (currentTenant.TenantId != null)
{
string? connectionString = configuration.GetConnectionString(currentTenant.TenantId);
optionsBuilder.UseNpgsql(connectionString);
}
base.OnConfiguring(optionsBuilder);
}
}
}