How to delete data in DB efficiently using LinQ to NHibernate (one-shot-delete)

Posted by kastanf on Stack Overflow See other posts from Stack Overflow or by kastanf
Published on 2011-01-17T09:50:48Z Indexed on 2011/01/17 9:53 UTC
Read the original article Hit count: 269

Hello,

producing software for customers, mostly using MS SQL but some Oracle, a decision was made to plunge into Nhibernate (and C#).

The task is to delete efficiently e.g. 10 000 rows from 100 000 and still stay sticked to ORM.

I've tried named queries - link already,

IQuery sql = s.GetNamedQuery("native-delete-car").SetString(0, "Kirsten");
sql.ExecuteUpdate();

but the best I have ever found seems to be:

 using (ITransaction tx = _session.BeginTransaction())
        {
            try
            {
                string cmd = "delete from Customer where Id < GetSomeId()";

                var count = _session.CreateSQLQuery(cmd).ExecuteUpdate();
                ...

Since it may not get into dB to get all complete rows before deleting them.

My questions are:

If there is a better way for this kind of delete.

If there is a possibility to get the Where condition for Delete like this:
Having a select statement (using LinQ to NHibernate) => which will generate appropriate SQL for DB => we get that Where condition and use it for Delete.

Thanks :-)

© Stack Overflow or respective owner

Related posts about sql

Related posts about nhibernate