Ashley John
Ashley John

Reputation: 2453

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>.

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. it just need to run an update statement alone in the DB with the values i provide.

so how i can i acheive that..

Upvotes: 3

Views: 660

Answers (1)

Daniel Schilling
Daniel Schilling

Reputation: 4967

NHibernate supports this type of bulk update via HQL. See 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();
    }
}

Upvotes: 2

Related Questions