EF用导航属性遍历从表时,删除主表出错

 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无法分辨该记录是否要保留。所以需要手动删除从表,不能再依赖级联删除

猜你喜欢

转载自www.cnblogs.com/Cyril-hcj/p/12975432.html