sharding/samples/Sample.Migrations/DefaultDesignTimeDbContextF...

53 lines
2.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.DependencyInjection;
using Sample.Migrations.EFCores;
using ShardingCore;
using ShardingCore.Bootstrappers;
2022-01-06 21:30:05 +08:00
using ShardingCore.TableExists;
using ShardingCore.TableExists.Abstractions;
namespace Sample.Migrations
{
public class DefaultDesignTimeDbContextFactory: IDesignTimeDbContextFactory<DefaultShardingTableDbContext>
{
private static IServiceProvider _serviceProvider;
static DefaultDesignTimeDbContextFactory()
{
2021-11-18 13:29:58 +08:00
var services = new ServiceCollection();
2022-01-06 21:30:05 +08:00
services.AddShardingDbContext<DefaultShardingTableDbContext>()
.AddEntityConfig(o =>
{
o.AddShardingTableRoute<ShardingWithModVirtualTableRoute>();
o.AddShardingTableRoute<ShardingWithDateTimeVirtualTableRoute>();
2022-01-06 21:30:05 +08:00
})
.AddConfig(op =>
{
op.UseShardingQuery((conStr, builder) =>
{
builder.UseSqlServer(conStr)
.ReplaceService<IMigrationsSqlGenerator, ShardingSqlServerMigrationsSqlGenerator<DefaultShardingTableDbContext>>()
.ReplaceService<IMigrationsModelDiffer, RemoveForeignKeyMigrationsModelDiffer>();
});
op.UseShardingTransaction((connection, builder) =>
{
builder.UseSqlServer(connection);
});
op.AddDefaultDataSource("ds0", "Data Source=localhost;Initial Catalog=ShardingCoreDBMigration;Integrated Security=True;");
}).ReplaceService<ITableEnsureManager,SqlServerTableEnsureManager>().EnsureConfig();
_serviceProvider = services.BuildServiceProvider();
}
public DefaultShardingTableDbContext CreateDbContext(string[] args)
{
return _serviceProvider.GetService<DefaultShardingTableDbContext>();
}
}
}