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;
|
|
|
|
|
|
|
|
|
|
namespace Sample.Migrations
|
|
|
|
|
{
|
|
|
|
|
public class DefaultDesignTimeDbContextFactory: IDesignTimeDbContextFactory<DefaultShardingTableDbContext>
|
|
|
|
|
{
|
|
|
|
|
static DefaultDesignTimeDbContextFactory()
|
|
|
|
|
{
|
|
|
|
|
var services = new ServiceCollection();
|
2021-10-11 20:58:55 +08:00
|
|
|
|
services.AddShardingDbContext<DefaultShardingTableDbContext>(
|
2021-10-15 17:18:23 +08:00
|
|
|
|
(conn, o) =>
|
|
|
|
|
o.UseSqlServer(conn)
|
2021-10-07 04:53:54 +08:00
|
|
|
|
.ReplaceService<IMigrationsSqlGenerator, ShardingSqlServerMigrationsSqlGenerator<DefaultShardingTableDbContext>>()
|
|
|
|
|
).Begin(o =>
|
|
|
|
|
{
|
2021-10-07 10:00:59 +08:00
|
|
|
|
o.CreateShardingTableOnStart = false;
|
|
|
|
|
o.EnsureCreatedWithOutShardingTable = false;
|
2021-10-07 04:53:54 +08:00
|
|
|
|
o.AutoTrackEntity = true;
|
|
|
|
|
})
|
2021-10-20 11:08:44 +08:00
|
|
|
|
.AddShardingTransaction((connection, builder) =>
|
|
|
|
|
builder.UseSqlServer(connection))
|
2021-10-07 04:53:54 +08:00
|
|
|
|
.AddDefaultDataSource("ds0",
|
|
|
|
|
"Data Source=localhost;Initial Catalog=ShardingCoreDBMigration;Integrated Security=True;")
|
|
|
|
|
.AddShardingTableRoute(o =>
|
|
|
|
|
{
|
|
|
|
|
o.AddShardingTableRoute<ShardingWithModVirtualTableRoute>();
|
|
|
|
|
o.AddShardingTableRoute<ShardingWithDateTimeVirtualTableRoute>();
|
|
|
|
|
}).End();
|
|
|
|
|
services.AddLogging();
|
|
|
|
|
var buildServiceProvider = services.BuildServiceProvider();
|
|
|
|
|
ShardingContainer.SetServices(buildServiceProvider);
|
|
|
|
|
new ShardingBootstrapper(buildServiceProvider).Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DefaultShardingTableDbContext CreateDbContext(string[] args)
|
|
|
|
|
{
|
2021-10-07 10:00:59 +08:00
|
|
|
|
return ShardingContainer.GetService<DefaultShardingTableDbContext>();
|
2021-10-07 04:53:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|