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); }