C#Nhibernate保存列表 [英] C# Nhibernate Save List

查看:69
本文介绍了C#Nhibernate保存列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我有一个包含40.000个寄存器的列表,需要将其保存在MSSQL DB中.当我尝试保存它时,我检查了控制台显示,我意识到它正在逐项保存,并且即使我试图这样做以插入下面带有comand的整个列表,也浪费了很多时间. >

Today I had an List with 40.000 registers that I needed to save in my MSSQL DB. When I tried to save it, I checked my console display and I realized that it was saving item by item, and that consumed a lot of time, even with me trying to do it to insert the entire list with the comand below.

List<Andamento> andamentoList = fillAndamentoList(parameters);
_repository.Save(andamentoList);
_repository.Commit();

是否可以配置NHibernate并将其更改为仅通过一次数据库交互即可实际插入整个列表?

Is it possible to configure NHibernate and change it to actually insert the entire list with only one database interaction?

非常感谢您

推荐答案

要使用NHibernate进行批处理,您需要使用无状态会话:

For batching using NHibernate you need to use stateless session:

using (var statelessSession = sessionFactory.OpenStatelessSession())
using (var transaction = statelessSession.BeginTransaction())
{
    foreach (var item in andamentoList)
    {
        statelessSession.Insert(item);
    } 
    transaction.Commit();
}

将其与批处理大小组合以在配置文件中获得性能:

Combine this with the batch size to gain performance in your config file:

<property name="adonet.batch_size">100</property>

您可以在此处阅读更多.

You can read more here.

这篇关于C#Nhibernate保存列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆