ABP EF Core 删除外键迁移

使用Code First模式时,会默认添加外键的迁移,外键约束虽然可以保证数据的一致性和完整性,但同样会带来一些问题,比如

  1. 可能带来一定性能影响,从表中插入或删除数据时,数据库都必须检查外键约束是否仍然有效。
  2. 可能会给运维带来困难,因为添加、删除或修改外键可能需要修改其他相关表。

如果想要EF默认不生成外键的迁移,可以使用如下方法。

修改EntityFrameworkCoreModule的ConfigureServices

修改前

public override void ConfigureServices(ServiceConfigurationContext context)
{
    context.Services.AddAbpDbContext<DbContext>(options =>
    {
        /* Add custom repositories here. Example:
         * options.AddRepository<Question, EfCoreQuestionRepository>();
         */
    });

    // 。。。其他代码
}

修改后

public override void ConfigureServices(ServiceConfigurationContext context)
{
    var configuration = context.Services.GetConfiguration();

    context.Services.AddDbContext<DbContext>(options =>
    {
        options.UseSqlServer(configuration["ConnectionStrings:Default"]);
        options.ReplaceService<IMigrationsModelDiffer, MigrationsModelDifferWithoutForeignKey>();
    });

    context.Services.AddAbpDbContext<DbContext>(options =>
    {
        /* Add custom repositories here. Example:
         * options.AddRepository<Question, EfCoreQuestionRepository>();
         */
    });

    // 。。。其他代码
}

定义类MigrationsModelDifferWithoutForeignKey

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "EF1001:Internal EF Core API usage.", Justification = "<挂起>")]
    public class MigrationsModelDifferWithoutForeignKey : MigrationsModelDiffer
    {
        public MigrationsModelDifferWithoutForeignKey
            ([NotNull] IRelationalTypeMappingSource typeMappingSource,
             [NotNull] IMigrationsAnnotationProvider migrationsAnnotations,
             IRowIdentityMapFactory rowIdentityMapFactory,
             CommandBatchPreparerDependencies commandBatchPreparerDependencies)
            : base(typeMappingSource, migrationsAnnotations, rowIdentityMapFactory, commandBatchPreparerDependencies)
        {
        }

        public override IReadOnlyList<MigrationOperation> GetDifferences(IRelationalModel? source, IRelationalModel? target)
        {
            var operations = base.GetDifferences(source, target)
                .Where(op => op is not AddForeignKeyOperation)
                .Where(op => op is not DropForeignKeyOperation)
                .ToList();

            foreach (var operation in operations.OfType<CreateTableOperation>())
                operation.ForeignKeys?.Clear();

            return operations;
        }
    }

此时生成的迁移就不会生成外键约束,但仍会保留索引的迁移。

posted @ 2023-05-25 19:47  cnblogsName  阅读(296)  评论(0编辑  收藏  举报