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 2022-09-19 23:53  donchen-c  阅读(596)  评论(0编辑  收藏  举报