How can I run NHibenate queries asynchronously?

后端 未结 5 797
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-30 23:15

One way to increase scalability of the server application is to run IO-bound operation (reading files, sockets, web requests, database requests etc) asynchronously. This doe

5条回答
  •  盖世英雄少女心
    2020-12-31 00:07

    As of NHibernate v5, async is now fully supported!

    Here are some nifty examples:

    Customer customer = await session.GetAsync(1);
    
    List customers = await session.Query().ToListAsync();
    
    Customer customer = await session.Query()
    .Where(x => x.Name.Contains("Customer 1"))
    .SingleOrDefaultAsync();
    

    Updating an entity

    using (ISession session = sessionFactory.OpenSession())
    using (ITransaction transaction = session.BeginTransaction())
    {
        Customer customer = await session.GetAsync(1);
        customer.Name = "Customer 3";
        await session.SaveOrUpdateAsync(customer);
        await transaction.CommitAsync();
    }
    

    Source article

提交回复
热议问题