Fluent nhibernate Fluent nhibernate批量保存

Fluent nhibernate Fluent nhibernate批量保存,fluent-nhibernate,Fluent Nhibernate,我有一个xml文件,我正在读取该文件并更新数据库中的现有记录 我将xml读入一个c#对象列表,并在其中进行迭代,在数据库中找到相应的记录,然后根据xml/c#值进行更新 然后我保存这个对象——但是在fluent nihibernate中有没有办法将对象添加到列表(可能是1000条记录)并保存 一句话,这是性能问题,我比较担心-我一直在使用亚音速2和3,并选择了亚音速2,因为它快得多-但我只是想知道关于使用fluent nhibernate来实现这一点有什么看法-示例等会话有一个缓存,这正是您想要

我有一个xml文件,我正在读取该文件并更新数据库中的现有记录

我将xml读入一个c#对象列表,并在其中进行迭代,在数据库中找到相应的记录,然后根据xml/c#值进行更新

然后我保存这个对象——但是在fluent nihibernate中有没有办法将对象添加到列表(可能是1000条记录)并保存


一句话,这是性能问题,我比较担心-我一直在使用亚音速2和3,并选择了亚音速2,因为它快得多-但我只是想知道关于使用fluent nhibernate来实现这一点有什么看法-示例等

会话有一个缓存,这正是您想要的。如果在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();
}
使用(var tx=session.BeginTransaction())
{
常数int批量大小=1000;
IList myobjects=ReadInXML();
字典批量=新字典(批量大小);
for(int i=0;ix.Id).IsIn(batch.Keys)
.List();
foreach(实体中的var实体)
{
更新(实体,批次[entity.Id]);
}
//会话知道它加载的所有实体,不需要session.Update();
session.Flush();
//从会话缓存中删除所有已处理的实体以加快速度
session.Clear();
//准备下一批
batch.Clear();
}
tx.Commit();
}