ABP框架中的数据填充依赖关系

编程语言 2026-07-11

我尝试用DataSeed进行数据填充,但遇到了这个错误:SqlException: The MERGE statement conflicted with the FOREIGN KEY constraint "FK_SubCategories_Categories_CategoryId".

第一步,我尝试使用多个IDataSeedContributor(一个用于类别,一个用于子类别),据我所知我无法控制它们的执行顺序。
第二步,我尝试使用多个IDataSeeder和一个通用的IDataSeedContributor,the context is disposed when the second dataseeder runs
第三步,我尝试使用一个包含这两个类别和子类别的通用IDataSeedContributor,出现错误:SqlException: The MERGE statement conflicted with the FOREIGN KEY constraint "FK_SubCategories_Categories_CategoryId"。并开启autoSave:true:

await categoryRepository.InsertManyAsync(categories ,autoSave: true);

autoSave:true 的含义是在单元工作结束前保存数据(不推荐,但有时确实需要)。正如文档中所述:

Note-1: All changes are automatically saved when a unit of work ends without any error. So, don't call SaveChangesAsync() and don't set autoSave to true unless you really need it.

但它似乎并没有按预期工作,仍然出现相同的外键错误。

如有帮助,将不胜感激。

解决方案

我通过使用导航属性解决了,效果棒极了。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章