c# – Linq to Entities删除而不提取

这段代码可以:
using(DbContext db = new DbContext())
{
    IEnumerable<Setting> settings = db.Settings.Where(s=> s.UserId==Uid);
    db.Settings.RemoveRange(settings); 
}

是以某种方式写的,不需要先取出?就像是:

using(DbContext db = new DbContext())
{
    db.Settings.Remove(s=> s.UserId==Uid); 
}

解决方法

您可以查看 EntityFramework.Extended库,它可以让您编写以下查询
//delete all Settings where UserId matches
db.Settings.Where(s=> s.UserId == Uid).Delete();

文档:

A current limitations of the Entity Framework is that in order to update or delete an entity you have to first retrieve it into memory. Now in most scenarios this is just fine. There are however some scenarios where performance would suffer. Also,for single deletes,the object must be retrieved before it can be deleted requiring two calls to the database. Batch update and delete eliminates the need to retrieve and load an entity before modifying it.

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么