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

前端之家收集整理的这篇文章主要介绍了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.

原文链接:https://www.f2er.com/csharp/244086.html

猜你在找的C#相关文章