简体   繁体   English

在NHibernate中多次保存(2.1)

[英]Multiple Saves in NHibernate (2.1)

How can I save a collection of objects in NHibernate? 如何在NHibernate中保存对象集合? I'm migration from SubSonic (I don't like SubSonic 3 version and SubSonic 2 is dead...) and this used to be a simple operation... 我正在从SubSonic迁移(我不喜欢SubSonic 3版本,SubSonic 2已经死了...),这以前是一个简单的操作...

There is a way to map a collection(without associations) to complete this task? 有没有办法映射一个集合(没有关联)来完成此任务?

My actual code is: 我的实际代码是:

using (ISession session = NHibernateHelper.GetCurrentSession())
{
    using (ITransaction transaction = session.BeginTransaction())
    {
        foreach (var player in players)
        {
             session.Save(player);
             transaction.Commit();
        }
    }
}

Thanks in advance! 提前致谢!

You need to commit the transaction outside of your loop. 您需要在循环之外提交事务。 The goal of a transaction is to essentially batch multiple operations to the database in 1 call. 事务的目标是实质上在1个调用中将多个操作批处理到数据库。 Here's the edited version: 这是编辑后的版本:

using (ISession session = NHibernateHelper.GetCurrentSession())
{
    using (ITransaction transaction = session.BeginTransaction())
    {
        foreach (var player in players)
        {
            session.Save(player);
        }
        transaction.Commit();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM