如何使用nhibernate批量删除? [英] How to bulk delete with nhibernate?

查看:91
本文介绍了如何使用nhibernate批量删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不首先拉入内存中所有对象的情况下使用nhibernate删除项目?

How can I delete items with nhibernate without first pulling all the objects in memory?

这是可能的还是我必须使用原始sql?

Is this possible or do I have to use raw sql?

推荐答案

使用ExecuteUpdate方法.下面的代码将批量提交批量删除.这适用于NHibernate 2.1.0. (不确定以前的版本)

Use the ExecuteUpdate method. The code below will commit bulk deletion in batches. This works in NHibernate 2.1.0. (Not sure about previous versions)

        foreach (List<int> batch in GetBatches(records, _batchSize))
        {
            using (ITransaction transaction = _session.BeginTransaction())
            {
                _session.CreateQuery(String.Format("DELETE  FROM {0} WHERE Id IN (:idsList)", _domainObject.Name))
                        .SetParameterList("idsList", batch.ToArray())
                        .ExecuteUpdate();

                transaction.Commit();
            }
        }

这篇关于如何使用nhibernate批量删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆