会话有一个缓存,这是你想要的。如果您在配置sessionfactory时启用了批处理,那么以下操作将批量更新
using (var tx = session.BeginTransaction())
{
const int BATCH_SIZE = 1000;
IList<MyObjects> myobjects = ReadInXML();
Dictionary<int, MyObjects> batch = new Dictionary<int, MyObjects>(BATCH_SIZE);
for (int i = 0; i < myobjects.Count; i++)
{
batch.Add(myobjects[i].Id, myobjects[i]);
if (batch.Count < BATCH_SIZE)
continue;
var entities = session.QueryOver<MyEntity>()
.WhereRestrictionOn(x => x.Id).IsIn(batch.Keys)
.List();
foreach (var entity in entities)
{
Update(entity, batch[entity.Id]);
}
// the session knows all entities it has loaded, no need for session.Update();
session.Flush();
// remove all processed entities from the session cache to speed up things
session.Clear();
// prepare for next batch
batch.Clear();
}
tx.Commit();
}