我想我知道这个问题的答案可能是什么,但我想我还是会继续问这个问题。

如果我做这样的事情,似乎在 NHibernate 中:

IList<Customer> customers = Session.CreateCriteria(typeof(Customer))
                                .Add(Restrictions.Eq("Name", "Steve")
                                .List<Customer>();

然后我想删除该客户列表。据我所知,唯一的方法是这样的:
foreach(var customer in customers)
{
    Session.Delete(customer);
}

但我想知道是否有什么办法可以去:
Session.Delete(customers);

并通过一次调用删除整个集合?

最佳答案

不使用 Criteria,但使用 HQL 很容易:

session.CreateQuery("delete Customer customer where customer in (:customers)")
       .SetParameterList("customers", customers.ToArray())
       .ExecuteUpdate();

但是您不需要加载它们。您也可以一次性完成:
session.CreateQuery("delete Customer where Name = 'Steve'")
       .ExecuteUpdate();

关于nhibernate - 使用 Criteria API 使用 NHibernate 删除集合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2749118/

10-11 15:44
查看更多