删除与孩子的记录 [英] remove a record with child

查看:69
本文介绍了删除与孩子的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

       //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.

MSDN支持和不支持的LINQ方法(LINQ到实体)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆