流利的nhibernate批量保存 [英] Fluent nhibernate batch save

查看:221
本文介绍了流利的nhibernate批量保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我将xml读入c#对象列表,然后遍历它们,找到在数据库中的相应记录,并从xml / c#值更新它。

然后我保存这个对象 - 但有没有什么办法流利的nihibernate添加对象一个列表(可能是1000年的记录)和bacth保存

底线是表现我更afer - 我一直在使用亚音速2和3,选择亚音速2因为它的方式更快 - 但我只是想知道使用流利的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;
$ b $ var entities = session.QueryOver< MyEntity>()
.WhereRestrictionOn(x => x.Id).IsIn(batch.Keys)
.List();

foreach(实体中的实体)
{
Update(entity,batch [entity.Id]);
}

//会话知道它已经加载的所有实体,不需要session.Update();
session.Flush();
//从会话缓存中删除所有已处理的实体以加快速度
session.Clear();
//准备下一批
batch.Clear();
}

tx.Commit();
}


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

解决方案

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();
}

这篇关于流利的nhibernate批量保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆