NHibernate:一次插入多个项目 [英] NHibernate: insert multiple items at once
问题描述
全部!我现在正在学习NHibernate,我想知道是否可以在一个操作中将多个对象保存到数据库中.
all! I am learning NHibernate now and I would like to know is it possible to save multiple objects to database in one operation.
例如,考虑此测试代码
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);
}
此循环在此处生成许多INSERT语句,这些语句在SQL Server 2008中可能不是最优的,从而允许在一个INSERT语句中包含多个数据行.
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!
推荐答案
有些东西接近您想要的东西.
There's something close to what you want.
如果将adonet.batch_size
配置属性设置为0(默认值)以外的任何值,则NHibernate将一次将那么多的语句发送到SQL Server.
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.
这篇关于NHibernate:一次插入多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!