问题
I have xml file that I am reading in and uppdating existing records in a database.
I read the xml into a list of c# objects and iterate through them finding the corresponding record in the datbase and update it from the xml/c# values.
I then save this object - but is there any way in fluent nihibernate to add the object to a list ( could be 1000s of records) and bacth save
Bottom line it is performance I am afer - I have been using subsonic 2 and 3 and have opted for subsonic 2 as it was way faster - but I was just wonder any views on using fluent nhibernate to do this - examples etc
回答1:
the session has a cache which is what you want. if you have batching enabled in the configuration of the sessionfactory then the following would batch the updates
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();
}
来源:https://stackoverflow.com/questions/8047597/fluent-nhibernate-batch-save