mh_frame_sps/APT.Infrastructure.EF/Extensions/MySqlTenantExtension.cs
2026-04-07 13:47:52 +08:00

72 lines
3.0 KiB
C#

using System;
using APT.Infrastructure.Api;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace APT.Infrastructure.EF.Extensions
{
public static class MySqlTenantExtension
{
public static IServiceCollection AddMySqlPerConnection<TDbContext>(this IServiceCollection services,
string key = "default",
string connectionPrefix = "tenanted",
Action<DbContextOptionsBuilder> optionAction = null,
Action<IServiceProvider, string, DbContextOptionsBuilder> dbContextSetup = null)
where TDbContext : DbContext, ITenantDbContext
{
return services.AddDbPerConnection<TDbContext>(DbIntegrationType.Mysql, key, connectionPrefix,
optionAction, dbContextSetup ?? SetUpMySql<TDbContext>);
}
public static IServiceCollection AddMySqlPerConnection<TDbContext>(this IServiceCollection services,
Action<TenantSettings<TDbContext>> setupAction = null)
where TDbContext : DbContext, ITenantDbContext
{
return services.AddDbPerConnection<TDbContext>(CombineSettings(setupAction));
}
public static IServiceCollection AddMySqlPerTable<TDbContext>(this IServiceCollection services,
string key = "default",
string connectionName = "tenanted",
Action<DbContextOptionsBuilder> optionAction = null,
Action<IServiceProvider, string, DbContextOptionsBuilder> dbContextSetup = null)
where TDbContext : DbContext, ITenantDbContext
{
return services.AddDbPerTable<TDbContext>(DbIntegrationType.Mysql, key, connectionName,
optionAction, dbContextSetup ?? SetUpMySql<TDbContext>);
}
public static IServiceCollection AddMySqlPerTable<TDbContext>(this IServiceCollection services,
Action<TenantSettings<TDbContext>> setupAction = null)
where TDbContext : DbContext, ITenantDbContext
{
return services.AddDbPerTable<TDbContext>(CombineSettings(setupAction));
}
static Action<TenantSettings<TDbContext>> CombineSettings<TDbContext>(
Action<TenantSettings<TDbContext>> setupAction = null)
where TDbContext : DbContext, ITenantDbContext
{
return (settings) =>
{
settings.DbContextSetup = SetUpMySql<TDbContext>;
setupAction?.Invoke(settings);
};
}
internal static void SetUpMySql<TDbContext>(IServiceProvider serviceProvider, string connectionString,
DbContextOptionsBuilder optionsBuilder)
where TDbContext : DbContext, ITenantDbContext
{
var settings = serviceProvider.GetService<TenantSettings<TDbContext>>();
var tenant = serviceProvider.GetService<TenantInfo>();
optionsBuilder.UseMySql(connectionString, builder =>
{
builder.TenantBuilderSetup(serviceProvider, settings, tenant);
});
}
}
}