-30
@@ -1,30 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Convention
|
||||
{
|
||||
public class MigrationRemoveForeignKeysSqlGenerator : MigrationsSqlGenerator
|
||||
{
|
||||
public MigrationRemoveForeignKeysSqlGenerator(MigrationsSqlGeneratorDependencies dependencies, IMigrationsAnnotationProvider migrationsAnnotations) : base(dependencies)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void Generate(Microsoft.EntityFrameworkCore.Migrations.Operations.CreateTableOperation operation, IModel? model, MigrationCommandListBuilder builder, bool terminate = true)
|
||||
{
|
||||
operation.ForeignKeys.Clear();
|
||||
base.Generate(operation, model, builder, terminate);
|
||||
}
|
||||
|
||||
protected override void Generate(AddForeignKeyOperation operation, IModel? model, MigrationCommandListBuilder builder,
|
||||
bool terminate = true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
namespace IRaCIS.Core.Test
|
||||
{
|
||||
/// <summary>
|
||||
/// 这种方式不行
|
||||
/// </summary>
|
||||
public class NoForeignKeyConvention : IModelFinalizingConvention
|
||||
{
|
||||
public void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
|
||||
{
|
||||
foreach (var entityType in modelBuilder.Metadata.GetEntityTypes())
|
||||
{
|
||||
foreach (var foreignKey in entityType.GetForeignKeys())
|
||||
{
|
||||
// 设置删除行为为 NoAction,避免生成外键约束
|
||||
foreignKey.Builder.OnDelete(DeleteBehavior.NoAction);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Internal;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
|
||||
namespace IRaCIS.Core.Test
|
||||
{
|
||||
public class NoForeignKeyMigrationsSqlGenerator : MigrationsSqlGenerator
|
||||
{
|
||||
public NoForeignKeyMigrationsSqlGenerator(
|
||||
MigrationsSqlGeneratorDependencies dependencies) : base(dependencies)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
|
||||
{
|
||||
if (operation is AddForeignKeyOperation || operation is DropForeignKeyOperation)
|
||||
{
|
||||
// 跳过外键的生成
|
||||
return;
|
||||
}
|
||||
|
||||
base.Generate(operation, model, builder);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user