简体   繁体   English

更新记录Nhibernate和Iqueryable

[英]Update records Nhibernate and Iqueryable

Let say if i want to update all rows of a table having some status with another status.I am using a Repository pattern with Nhibernate .I have a method in my repository to get me an Iqueryable<T> . 假设我想要更新具有某种状态和其他状态的表的所有行。我正在使用带有Nhibernate的Repository模式。我在我的存储库中有一个方法让我得到一个Iqueryable<T>

So how do i write the update for that.I dont want to get all the elements ,convert to List and then update it.In fact i dont even want Nhibernate to select me the list of rows. 那么我如何编写更新。我不想获取所有元素,转换为List然后更新它。实际上我甚至不希望Nhibernate选择行列表。 it just need to run an update statement alone in the DB with the values i provide. 它只需要在DB中使用我提供的值单独运行更新语句。

so how i can i acheive that.. 所以我怎么能实现这一点..

NHibernate supports this type of bulk update via HQL. NHibernate通过HQL支持这种类型的批量更新。 See http://www.nhforge.org/doc/nh/en/index.html#batch-direct . http://www.nhforge.org/doc/nh/en/index.html#batch-direct Try something along the lines of this... 尝试一下这个......

public class ThingRepository : Repository<Thing>, IThingRepository
{
    public int UpdateStatus(ThingStatus oldStatus, ThingStatus newStatus)
    {
        var query = Session.CreateQuery("UPDATE Thing SET Status = :newStatus WHERE Status = :oldStatus")
            .SetEnum("oldStatus", oldStatus)
            .SetEnum("newStatus", newStatus);

        return query.ExecuteUpdate();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM