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;
|
2021-10-29 10:44:48 +08:00
|
|
|
|
using ShardingCore.Bootstrapers;
|
2021-10-07 04:53:54 +08:00
|
|
|
|
|
|
|
|
|
namespace Sample.Migrations
|
|
|
|
|
{
|
|
|
|
|
public class DefaultDesignTimeDbContextFactory: IDesignTimeDbContextFactory<DefaultShardingTableDbContext>
|
|
|
|
|
{
|
|
|
|
|
static DefaultDesignTimeDbContextFactory()
|
|
|
|
|
{
|
2021-11-18 13:29:58 +08:00
|
|
|
|
var services = new ServiceCollection();
|
|
|
|
|
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);
|
2021-11-02 15:35:15 +08:00
|
|
|
|
ShardingContainer.GetService<IShardingBootstrapper>().Start();
|
2021-10-07 04:53:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|