Run inline sql to update table using nHibernate

Sorry, if this was asked earlier, I did a search, but did not find anything.

Is it possible to execute inline sql in nHibernate? I have something like this that I would like to run against dB:

_session.CreateSQLQuery(
              @"update things
                set defaultThing = 0 where parentId = :parentId AND thingId <> :thingId")
                .SetInt32("parentId ", parent.Id)
                .SetInt32("thingId", thing.Id)
                ;

I assume that I could iterate over a bunch of “things” and set the defaultThing parameter to false and then call _session.Update(thing), but if I can do this, as I stated above, it will be great.

+3
source share
1 answer

Yes, just use ExecuteUpdate()for this query. It is equivalent IDbCommand.ExecuteNonQuery().

As mentioned above, you can also use HQL. Check 12.3. DML style operations

+4
source

Source: https://habr.com/ru/post/1788164/


All Articles