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

24 lines
979 B
C#

// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
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);
}
}
}