Wednesday, 28 June 2017

NHibernate: insert multiple items at once

For example, consider this test code
    private static void SaveTestBillNamesInSession(ISession session, params string[] names)
    {
        var bills = from name in names
                    select new Bill
                        {
                            Name = name,
                            DateRegistered = DateTime.Now,
                        };
        foreach (var bill in bills)
            session.SaveOrUpdate(bill);
    }
This loop here generates many INSERT statements which may be sub-optimal in SQL Server 2008 which allows to include multiple data rows in one INSERT statement.
Is it possible to rewrite this code to make use of this functionality - insert all the data in one operation?
There's something close to what you want.
If you set the adonet.batch_size configuration property to anything other than 0 (the default), NHibernate will send that many statements at once to SQL Server.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Blog Archive