English 中文(简体)
如何使用nhibernate进行批量删除?
原标题:How to bulk delete with nhibernate?
  • 时间:2010-02-18 23:28:15
  •  标签:
  • nhibernate

如果没有首先将所有物体射入记忆,我怎么能够删除原封不动的物品?

这是可能的吗,还是我必须使用原始SQL?

问题回答

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

        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 5开始,你可以使用下列合成物:

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

从NHibernate开始 5.0, Linq查询可用于插入、更新或删除实体。 问询界定了删除、更新或插入数据,然后删除、更新、更新、更新、加入和插入可质疑的推广方法,以便删除该数据,或指示以何种方式加以更新或插入。 这些询问完全在数据库内进行,没有从数据库中提取相应的实体。

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





相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签