public static IServiceCollection AddDatabasePerConnection <TDbContext>(this IServiceCollection services, string key = "default") where TDbContext : DbContext, ITenantDbContext { var option = new ConnectionResolverOption() { Key = key, Type = ConnectionResolverType.ByDatabase, }; return(services.AddDatabasePerConnection <TDbContext>(option)); }
public static IServiceCollection AddTenantDatabasePerTable <TDbContext>(this IServiceCollection services, string connectionStringName, string key = "default") where TDbContext : DbContext, ITenantDbContext { var option = new ConnectionResolverOption() { Key = key, Type = ConnectionResolverType.ByTabel, ConnectinStringName = connectionStringName }; return(services.AddTenantDatabasePerTable <TDbContext>(option)); }
public static IServiceCollection AddDatabasePerConnection <TDbContext>(this IServiceCollection services, ConnectionResolverOption option) where TDbContext : DbContext, ITenantDbContext { if (option == null) { option = new ConnectionResolverOption() { Key = "default", Type = ConnectionResolverType.ByDatabase, }; } return(services.AddDatabase <TDbContext>(option)); }
public static IServiceCollection AddTenantDatabasePerSchema <TDbContext>(this IServiceCollection services, string connectionStringName, string key = "default") where TDbContext : DbContext, ITenantDbContext { var option = new ConnectionResolverOption() { Key = key, Type = ConnectionResolverType.BySchema, ConnectinStringName = connectionStringName, DBType = DatabaseIntegration.SqlServer }; return(services.AddTenantDatabasePerSchema <TDbContext>(option)); }
public static IServiceCollection AddTenantDatabasePerSchema <TDbContext>(this IServiceCollection services, ConnectionResolverOption option) where TDbContext : DbContext, ITenantDbContext { if (option == null) { option = new ConnectionResolverOption() { Key = "default", Type = ConnectionResolverType.BySchema, ConnectinStringName = "default", DBType = DatabaseIntegration.SqlServer }; } return(services.AddTenantDatabasePerTable <TDbContext>(option)); }
internal static IServiceCollection AddDatabase <TDbContext>(this IServiceCollection services, ConnectionResolverOption option) where TDbContext : DbContext, ITenantDbContext { services.AddSingleton(option); services.AddScoped <TenantInfo>(); services.AddScoped <ISqlConnectionResolver, TenantSqlConnectionResolver>(); services.AddDbContext <TDbContext>((serviceProvider, options) => { var resolver = serviceProvider.GetRequiredService <ISqlConnectionResolver>(); var dbOptionBuilder = options.UseMySql(resolver.GetConnection()); if (option.Type == ConnectionResolverType.ByTabel) { dbOptionBuilder.ReplaceService <IModelCacheKeyFactory, TenantModelCacheKeyFactory <TDbContext> >(); } }); return(services); }
internal static IServiceCollection AddDatabase <TDbContext>(this IServiceCollection services, ConnectionResolverOption option) where TDbContext : DbContext, ITenantDbContext { services.AddSingleton(option); services.AddScoped <TenantInfo>(); services.AddScoped <ISqlConnectionResolver, TenantSqlConnectionResolver>(); services.AddDbContext <TDbContext>((serviceProvider, options) => { var dbContextManager = serviceProvider.GetService <IDbContextManager>(); var resolver = serviceProvider.GetRequiredService <ISqlConnectionResolver>(); DbContextOptionsBuilder dbOptionBuilder = null; switch (option.DBType) { case DatabaseIntegration.SqlServer: dbOptionBuilder = options.UseSqlServer(resolver.GetConnection()); break; case DatabaseIntegration.Mysql: dbOptionBuilder = options.UseMySql(resolver.GetConnection()); break; default: throw new System.NotSupportedException("db type not supported"); } if (option.Type == ConnectionResolverType.ByTabel || option.Type == ConnectionResolverType.BySchema) { dbOptionBuilder.ReplaceService <IModelCacheKeyFactory, TenantModelCacheKeyFactory <TDbContext> >(); } }); return(services); }
public TenantSqlConnectionResolver(TenantInfo tenantInfo, IConfiguration configuration, ConnectionResolverOption option) { this.option = option; this.tenantInfo = tenantInfo; this.configuration = configuration; }