NHibernate: insert multiple items at once

浪子不回头ぞ 提交于 2020-01-13 11:26:27

问题


all! I am learning NHibernate now and I would like to know is it possible to save multiple objects to database in one operation.

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?

Update OK, now it's really started to send everything in a single batch. Many thanks to all!


回答1:


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.



来源:https://stackoverflow.com/questions/2833406/nhibernate-insert-multiple-items-at-once

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!