C # Nhibernate Save List

Today I had a list with 40,000 registers that I needed to save in my MSSQL DB. When I tried to save it, I checked the console screen and realized that it saves item by item, and it consumes a lot of time, even when I try to do this to insert the entire list using the command below.

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

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

Many thanks,

+1
c # sql-server nhibernate
source share
2 answers

For batch processing using NHibernate, you need to use a 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 batch size to improve performance in your configuration file:

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

You can read it here .

+2
source share

First of all. If you do not start an explicit transaction, NHibernate will start implicit for each item you save / discard. Starting a transaction is expensive. So the first thing you need to do is

 using (var transaction = session.BeginTransaction()) { //Loop here transaction.Commit(); } 

Another thing to pay attention to is how many operators are required to store one object. If you have more than one, you need to create your own dispenser (the built-in cannot handle the tables well for the class and other cases).

0
source share

All Articles