-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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using IRaCIS.Core.Test.CodeFirstTest.MSSQL;
|
||||
using IRaCIS.Core.Test.CodeFirstTest.MSSQL.Convention;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.Extensions.Options;
|
||||
@@ -42,8 +41,9 @@ public partial class IRCContext : DbContext
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
//移除外键约束
|
||||
optionsBuilder.ReplaceService<IMigrationsSqlGenerator, MigrationRemoveForeignKeysSqlGenerator>();
|
||||
|
||||
// 替换默认的 MigrationsSqlGenerator
|
||||
optionsBuilder.ReplaceService<IMigrationsSqlGenerator, NoForeignKeyMigrationsSqlGenerator>();
|
||||
optionsBuilder.UseSqlServer("Server=106.14.89.110,1433;Database=IRC_Code;User ID=sa;Password=mssql_KnTs2a;TrustServerCertificate=true");
|
||||
|
||||
|
||||
@@ -59,12 +59,14 @@ public partial class IRCContext : DbContext
|
||||
modelBuilder.ApplyConfiguration(configurationInstance);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
OnModelCreatingPartial(modelBuilder);
|
||||
}
|
||||
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
|
||||
{
|
||||
|
||||
|
||||
//针对字符串使用默认的长度配置
|
||||
configurationBuilder.Conventions.Add(_ => new MaxStringLengthConvention());
|
||||
}
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations
|
||||
{
|
||||
[DbContext(typeof(IRCContext))]
|
||||
[Migration("20240912012757_RemoveForeignKey")]
|
||||
[Migration("20240912054434_RemoveForeignKey")]
|
||||
partial class RemoveForeignKey
|
||||
{
|
||||
/// <inheritdoc />
|
||||
@@ -151,16 +151,21 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL
|
||||
#region 测试生成迁移移除外键约束
|
||||
|
||||
#endregion
|
||||
|
||||
#region 测试迁移不生成外键约束,ef知道外键关系,但是数据库不保存外键
|
||||
public class Project : BaseFullAuditEntity
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class ProjectUser: BaseFullAuditEntity
|
||||
public class ProjectUser : BaseFullAuditEntity
|
||||
{
|
||||
//外键
|
||||
public Guid ProjectId { get; set; }
|
||||
|
||||
public Project Project { get; set; }
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user