添加移除外键的迁移

This commit is contained in:
xuejmnet 2021-11-28 21:37:41 +08:00
parent ac86389d53
commit 058df99f26
2 changed files with 41 additions and 0 deletions

View File

@ -21,6 +21,7 @@ namespace Sample.Migrations
(conn, o) =>
o.UseSqlServer(conn)
.ReplaceService<IMigrationsSqlGenerator, ShardingSqlServerMigrationsSqlGenerator<DefaultShardingTableDbContext>>()
.ReplaceService<IMigrationsModelDiffer,RemoveForeignKeyMigrationsModelDiffer>()
).Begin(o =>
{
o.CreateShardingTableOnStart = false;

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Internal;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Update;
using Microsoft.EntityFrameworkCore.Update.Internal;
namespace Sample.Migrations
{
/*
* @Author: xjm
* @Description:
* @Date: Sunday, 28 November 2021 21:35:45
* @Email: 326308290@qq.com
*/
[SuppressMessage("Usage", "EF1001:Internal EF Core API usage.", Justification = "<挂起>")]
public class RemoveForeignKeyMigrationsModelDiffer:MigrationsModelDiffer
{
public RemoveForeignKeyMigrationsModelDiffer(IRelationalTypeMappingSource typeMappingSource, IMigrationsAnnotationProvider migrationsAnnotations, IChangeDetector changeDetector, IUpdateAdapterFactory updateAdapterFactory, CommandBatchPreparerDependencies commandBatchPreparerDependencies) : base(typeMappingSource, migrationsAnnotations, changeDetector, updateAdapterFactory, commandBatchPreparerDependencies)
{
}
public override IReadOnlyList<MigrationOperation> GetDifferences(IRelationalModel? source, IRelationalModel? target)
{
var sourceOperations = base.GetDifferences(source, target).ToList();
sourceOperations.RemoveAll(x => x is AddForeignKeyOperation || x is DropForeignKeyOperation);
foreach (var operation in sourceOperations.OfType<CreateTableOperation>())
{
operation.ForeignKeys?.Clear();
}
return sourceOperations;
}
}
}