2021-10-07 04:53:54 +08:00
|
|
|
|
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;
|
2022-06-17 10:57:25 +08:00
|
|
|
|
using ShardingCore.Bootstrappers;
|
2022-01-06 21:30:05 +08:00
|
|
|
|
using ShardingCore.TableExists;
|
2022-07-03 16:52:03 +08:00
|
|
|
|
using ShardingCore.TableExists.Abstractions;
|
2021-10-07 04:53:54 +08:00
|
|
|
|
|
|
|
|
|
namespace Sample.Migrations
|
|
|
|
|
{
|
|
|
|
|
public class DefaultDesignTimeDbContextFactory: IDesignTimeDbContextFactory<DefaultShardingTableDbContext>
|
2022-07-03 16:52:03 +08:00
|
|
|
|
{
|
|
|
|
|
private static IServiceProvider _serviceProvider;
|
2021-10-07 04:53:54 +08:00
|
|
|
|
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 =>
|
2021-10-07 04:53:54 +08:00
|
|
|
|
{
|
|
|
|
|
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;");
|
|
|
|
|
|
2022-07-03 16:52:03 +08:00
|
|
|
|
}).ReplaceService<ITableEnsureManager,SqlServerTableEnsureManager>().EnsureConfig();
|
|
|
|
|
_serviceProvider = services.BuildServiceProvider();
|
2021-10-07 04:53:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DefaultShardingTableDbContext CreateDbContext(string[] args)
|
|
|
|
|
{
|
2022-07-03 16:52:03 +08:00
|
|
|
|
return _serviceProvider.GetService<DefaultShardingTableDbContext>();
|
2021-10-07 04:53:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|