How to delete content using nhibernate?

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

Is this possible, or do I need to use raw sql?

+5
source share
2 answers

Use the ExecuteUpdate method. The code below will perform bulk removal of packages. 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();
            }
        }
+8
source

Starting with NHibernate 5, you can use the following syntax:

session.Query<Cat>()
    .Where(c => c.BodyWeight > 20)
    .Delete();

NHibernate 5.0, Linq , . , , "", "", "UpdateBuilder", "InsertInto" "InsertBuilder" ) , . , .

: http://nhibernate.info/doc/nhibernate-reference/querylinq.html#querylinq-modifying

0

All Articles