Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bulk delete with nhibernate?

Tags:

nhibernate

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

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

like image 730
Blankman Avatar asked Feb 18 '10 23:02

Blankman


1 Answers

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();
            }
        }
like image 142
Newbie Avatar answered Oct 12 '22 01:10

Newbie