ABP EF Core 删除外键迁移
使用Code First模式时,会默认添加外键的迁移,外键约束虽然可以保证数据的一致性和完整性,但同样会带来一些问题,比如
- 可能带来一定性能影响,从表中插入或删除数据时,数据库都必须检查外键约束是否仍然有效。
- 可能会给运维带来困难,因为添加、删除或修改外键可能需要修改其他相关表。
如果想要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;
}
}
此时生成的迁移就不会生成外键约束,但仍会保留索引的迁移。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构