EF用导航属性遍历从表时,删除主表出错
- 2020 年 5 月 27 日
- 筆記
var entitys= Repository.Table.Where(a => ids.Contains(a.UUID)).ToList(); entitys.ForEach(a => { foreach (var deductionitem in a.Deductionitems){} DoDelete(a); }
DeductionItems和主表配了级联删除,代码执行时会抛The relationship could not be changed because one or more of the foreign-key properties is non-nullable
解决方法:
遍历子表后,EF无法分辨该记录是否要保留。所以需要手动删除从表,不能再依赖级联删除