这段代码可以:
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.