40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using System;
|
|
using APT.Infrastructure.Api;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace APT.Infrastructure.EF
|
|
{
|
|
public abstract class TenantBaseDbContext : DbContext, ITenantDbContext
|
|
{
|
|
public TenantInfo Tenant { get; protected internal set; }
|
|
private readonly IServiceProvider serviceProvider;
|
|
public TenantBaseDbContext(DbContextOptions options, TenantInfo tenant, IServiceProvider serviceProvider)
|
|
: base(options)
|
|
{
|
|
this.serviceProvider = serviceProvider;
|
|
this.Tenant = tenant;
|
|
}
|
|
public TenantBaseDbContext()
|
|
: base()
|
|
{
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
if (!string.IsNullOrEmpty(Tenant?.Conn))
|
|
{
|
|
var builderType = typeof(ITenantEntityBuilder<>).MakeGenericType(this.GetType());
|
|
|
|
ITenantEntityBuilder entityBuilder = (ITenantEntityBuilder)serviceProvider.GetService(builderType);
|
|
|
|
entityBuilder.UpdateEntities(modelBuilder);
|
|
}
|
|
else
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|