2011-11-08 105 views
0

我有XML文件,我正在阅读和uppdating数据库中的现有记录。流利的nhibernate批保存

我将xml读入c#对象列表中并遍历它们,找到datbase中的相​​应记录并从xml/c#值更新它。

我然后保存这个对象 - 但有没有办法用流利的nihibernate对象添加到列表(可记录1000)和bacth保存

底线是性能我AFER - 我有一直在使用亚音速2和3,并选择亚音速2,因为它速度更快 - 但我只是想知道如何使用流畅的nhibernate来做到这一点 - 例子等

回答

0

会话有一个缓存,这是你想要的。如果您在配置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(); 
}