Example #1
0
        internal static void SetUpSqlServer <TDbContext>(IServiceProvider serviceProvider,
                                                         DbContextOptionsBuilder optionsBuilder,
                                                         Action <TenantSettings <TDbContext> > setupAction = null)
            where TDbContext : DbContext, ITenantDbContext
        {
            var settings = optionsBuilder.InitSettings <TDbContext>(serviceProvider, setupAction);

            settings.DbType = DbIntegrationType.SqlServer;

            var connectionResolver = serviceProvider.GetService <ITenantConnectionResolver <TDbContext> >();

            var tenant = serviceProvider.GetService <TenantInfo>();

            optionsBuilder.UseSqlServer(connectionResolver.GetConnection(), builder =>
            {
                if (settings.ConnectionType == ConnectionResolverType.ByTable)
                {
                    builder.MigrationsHistoryTable($"{tenant.Name}__EFMigrationsHistory");
                }
                if (settings.ConnectionType == ConnectionResolverType.BySchema)
                {
                    builder.MigrationsHistoryTable("__EFMigrationHistory", $"{(settings.SchemaFunc?.Invoke(tenant) ?? tenant.Name)}");
                }
            });

            optionsBuilder.ReplaceServiceTenanted(settings);
            settings.DbContextOptionAction?.Invoke(optionsBuilder);
        }