sharding/samples/Samples.DynamicDb.Npgsql/WebApplication1.Data/ShardingMigrationsSqlGenera...

38 lines
1.7 KiB
C#
Raw Normal View History

2022-06-21 16:04:56 +08:00
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Migrations;
2022-07-13 08:47:19 +08:00
using ShardingCore.Core.RuntimeContexts;
2022-06-21 16:04:56 +08:00
using ShardingCore.Helpers;
using ShardingCore.Sharding.Abstractions;
using System.Linq;
namespace WebApplication1.Data
{
/// <summary>
/// https://github.com/Coldairarrow/EFCore.Sharding/blob/master/src/EFCore.Sharding.SqlServer/ShardingSqlServerMigrationsSqlGenerator.cs
/// </summary>
public class ShardingMigrationsSqlGenerator<TShardingDbContext> : NpgsqlMigrationsSqlGenerator where TShardingDbContext : DbContext, IShardingDbContext
{
2022-07-13 08:47:19 +08:00
private readonly IShardingRuntimeContext runtimeContext;
2022-06-21 16:04:56 +08:00
2022-07-13 08:47:19 +08:00
public ShardingMigrationsSqlGenerator(MigrationsSqlGeneratorDependencies dependencies, INpgsqlSingletonOptions npgsqlSingletonOptions, IShardingRuntimeContext runtimeContext) : base(dependencies, npgsqlSingletonOptions)
2022-06-21 16:04:56 +08:00
{
2022-07-13 08:47:19 +08:00
this.runtimeContext = runtimeContext;
2022-06-21 16:04:56 +08:00
}
protected override void Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
{
var oldCmds = builder.GetCommandList().ToList();
base.Generate(operation, model, builder);
var newCmds = builder.GetCommandList().ToList();
var addCmds = newCmds.Where(x => !oldCmds.Contains(x)).ToList();
2022-07-13 08:47:19 +08:00
MigrationHelper.Generate(runtimeContext, operation, builder, Dependencies.SqlGenerationHelper, addCmds);
2022-06-21 16:04:56 +08:00
}
}
}