删除与孩子的记录 [英] remove a record with child
问题描述
//Delete Last Record which has income
var itemToRemove = db.People.LastOrDefault(p => p.Incomes.Any());
db.Incomes.RemoveRange(itemToRemove.Incomes);
db.People.Remove(itemToRemove);
它给了我这个错误
在EntityFramework.SqlServer.dll中发生了'System.NotSupportedException'类型的未处理异常
An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll
其他信息:LINQ to Entities无法识别方法'EF_Examples_02.Person LastOrDefault [Person]
Additional information: LINQ to Entities does not recognize the method 'EF_Examples_02.Person LastOrDefault[Person]
我有两个表(人,收入),每个人可以有n个收入. 两个表有关系.
I have two Tables(Person,Income) each person can have n incomes . two Tables have relationship.
推荐答案
问题出在LastOrDefault中. Ling-to-Entities不支持.
The problem is in LastOrDefault. Ling-to-Entities does not support it.
您必须注意,不能将多个Enumerable函数作为Queryable执行.
You have to be aware that several Enumerable functions can't be performed as Queryable.
LastOrDefault:不支持
LastOrDefault: Not supported
幸运的是,它支持FirstOrDefault.
Luckily, FirstOrDefault is supported.
通常,如果没有任何顺序搜索第一/最后",结果将是不可预测的.
Normally the result is fairly unpredictable if you search for First / Last without any ordering.
请考虑为自己指定您将称为Last元素的名称,此后很容易按降序排列,因此它就是您的First元素,可以查询.
Consider specifying for yourself what you would call the Last element, after that it is easy to order in descending order, so it would be your First element, which can be queried.
这篇关于删除与孩子的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!