Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate Linq in read-only mode

Tags:

nhibernate

Is it possible to set read-only mode on entities that are loaded using NHibernate's Linq provider?

For example, the following can be done with Session.QueryOver (and I believe with Criteria as well):

Session.QueryOver(Of Foo)().ReadOnly()

Is there an equivilent for Session.Query available?

like image 348
DanP Avatar asked May 29 '13 14:05

DanP


2 Answers

As stated in the documentation 10.1.2. Loading persistent entities as read-only:

To change the default behavior so NHibernate loads entity instances of mutable classes into the session and automatically makes them read-only, call:

Session.DefaultReadOnly = true;

To change the default back so entities loaded by NHibernate are not made read-only, call:

Session.DefaultReadOnly = false;

So before calling Session.Query... call Session.DefaultReadonly = true, because this setting goes to the ISession not to the Provider on top of it.

like image 141
Radim Köhler Avatar answered Nov 15 '22 10:11

Radim Köhler


if you need read only session, better approach is here: how to create a readonly session in nHiberate?

Session.DefaultReadOnly = true; - in this case NHibernate will accumulate all updates for entities (call all listeners e.t.c.)

session.FlushMode = FlushMode.Never

in this case NHibernate will not call any listeners, as I know

like image 44
razon Avatar answered Nov 15 '22 10:11

razon