我对EF Core中的某些(对我来说)奇怪行为有疑问:
以下面的代码为例:
efOrder = _context.Orders.ActiveOnly().FirstOrDefault(ord => ord.Id == id); //ActiveOnly() filters out my soft deleted orders
//No orderlines here
_context.Set<OrderLine>().RemoveRange(_context.OrderLine.ActiveOnly().Where(ol => ol.OrderId == efOrder.Id));
//sudden orderlines on efOrder here
为什么我的efOrder
突然出现OrderLines
时我没有将其添加到efOrder
?
因为这偶然会导致切断的关联错误,我该如何防止这种行为?
答案 0 :(得分:0)
解决方案是在不跟踪查询查询的情况下运行查询;
efOrder = _context.Orders.ActiveOnly().FirstOrDefault(ord => ord.Id == id);
//ActiveOnly() filters out my soft deleted orders
//No orderlines here
_context.Set<OrderLine>().RemoveRange(_context.OrderLine.AsNoTracking().ActiveOnly().Where(ol => ol.OrderId == efOrder.Id));
//Orderlines are not added
感谢您的帮助。