简体   繁体   中英

Multiple Saves in NHibernate (2.1)

How can I save a collection of objects in 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...

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. 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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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