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

56 lines
2.1 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;
2022-07-29 16:26:02 +08:00
[Obsolete("Obsolete")]
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) =>
{
2022-07-29 16:26:02 +08:00
builder.UseSqlServer(conStr);
2022-01-06 21:30:05 +08:00
});
op.UseShardingTransaction((connection, builder) =>
{
builder.UseSqlServer(connection);
});
op.AddDefaultDataSource("ds0", "Data Source=localhost;Initial Catalog=ShardingCoreDBMigration;Integrated Security=True;");
2022-07-29 16:26:02 +08:00
op.UseShardingMigrationConfigure(op =>
{
op.ReplaceService<IMigrationsSqlGenerator,
2023-02-22 14:19:11 +08:00
ShardingSqlServerMigrationsSqlGenerator>();
2022-07-29 16:26:02 +08:00
});
2023-02-22 14:19:11 +08:00
}).EnsureConfig();
_serviceProvider = services.BuildServiceProvider();
}
public DefaultShardingTableDbContext CreateDbContext(string[] args)
{
return _serviceProvider.GetService<DefaultShardingTableDbContext>();
}
}
}