随笔 - 130  文章 - 0  评论 - 7  阅读 - 32万

EFCore 6级联删除问题解决Database operation expected to affect 1 row(s) but actually affected 0 row(s)

异常信息:

Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.

处理方案:

复制代码
public async Task DeleteAnimalInfo(string animalToken)
        {
            var dbContext = new DBContext(_configuration);
            var _animalInfoRepository = new EFRepository<AnimalInfo>(dbContext);
            var query = _animalInfoRepository .Table;
            query = query.Where(x => x.AnimalToken.Equals(animalToken));
            if (query.Count() <= 0)
            {
                throw new Exception("does not exist in the database");
            }
            var entity = query.FirstOrDefault();

            _animalInfoRepository .Delete(query);

        }
View Code
复制代码

 

posted on   donchen-c  阅读(630)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示