NHibernate 批量更新 [英] Batch Update in NHibernate

查看:26
本文介绍了NHibernate 批量更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NHibernate 中是否存在批量更新命令?据我所知,它没有.那么处理这种情况的最佳方法是什么?我想执行以下操作:

Does batch update command exist in NHibernate? As far as I am aware it doesn't. So what's the best way to handle this situation? I would like to do the following:

  1. 从数据库中获取对象列表(我们称它们为用户列表,List)
  2. 更改这些对象的属性,( Users.Foreach(User=>User.Country="Antartica")
  3. 单独更新每个项目(Users.Foreach(User=>NHibernate.Session.Update(User)).
  4. 调用 Session.Flush 来更新数据库.
  1. Fetch a list of objects ( let's call them a list of users, List<User> ) from the database
  2. Change the properties of those objects, ( Users.Foreach(User=>User.Country="Antartica")
  3. Update each item back individually ( Users.Foreach(User=>NHibernate.Session.Update(User)).
  4. Call Session.Flush to update the database.

这是一个好方法吗?这会导致我的代码和数据库之间有很多往返吗?

Is this a good approach? Will this resulted in a lot of round trip between my code and the database?

你怎么看?或者有更优雅的解决方案吗?

What do you think? Or is there a more elegant solution?

推荐答案

启动 NHibernate 3.2 批处理作业有一些改进,可以最大限度地减少数据库往返.更多信息可以在 HunabKu 博客上找到.这是它的示例 - 这些批量更新仅执行 6 次往返:

Starting NHibernate 3.2 batch jobs have improvements which minimizes database roundtrips. More information can be found on HunabKu blog. Here is example from it - these batch updates do only 6 roundtrips:

using (ISession s = OpenSession())
using (s.BeginTransaction())
{
    for (int i = 0; i < 12; i++)
    {
        var user = new User {UserName = "user-" + i};
        var group = new Group {Name = "group-" + i};
        s.Save(user);
        s.Save(group);
        user.AddMembership(group);
    }
    s.Transaction.Commit();
}

这篇关于NHibernate 批量更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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