且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从数据库中删除满足条件的实体的最有效方法是什么?

更新时间:2023-11-25 11:39:34

我认为 ExecuteSqlRaw 更适合您:

_context.Database.ExecuteSqlRaw("DELETE FROM Users WHERE Name = @p0", "John");

通常 Remove RemoveRange 适用于您已经具有 Primary Key (通常是 Id )的情况对象,在这种情况下,您可以执行以下操作:

Generally Remove and RemoveRange are suitable for situations which you already have Primary Key (usually Id) of objects, in those cases you can do something like this:

_context.Users.Remove(new User {Id = id});