Hi,

This is documented on nhibernate.info, documentation menu, Current 
Reference, The NHibernate reference, updating detached objects 
<https://nhibernate.info/doc/nhibernate-reference/manipulatingdata.html#manipulatingdata-updating-detached>.
 
NHibernate sessions have an Update method meant to attach entities to the 
session. (While Save is meant to add new entities in the session.) There is 
also a SaveOrUpdate to let NHibernate do the appropriate action, if the 
entity mapping allows to distinguish transient entities from detached ones. 
That is documented at the same place. Also pay attention to the cascade 
mapping property on entities, if you want "updates" (reattaching entities) 
to be cascade to relations of an updated entity. See 
https://nhibernate.info/doc/nhibernate-reference/manipulatingdata.html#manipulatingdata-graphs
.

This Update method is not to be confused with the SQL update statement. It 
is no meant to trigger immediate SQL updates. The SQL operations to be 
performed on entities will happen on session flush as usual.

Instead of attaching entities in another session, you may use session 
disconnection 
<https://nhibernate.info/doc/nhibernate-reference/transactions.html#transactions-disconnection>
 
and reconnect it for flushing after user interaction. That is a simpler 
approach, but not usable with all architectures, like having the entities 
loaded by a service, serialized and transmitted to a client, sent back by 
the client modified: of course in such case the entities sent back are not 
attached to the original session. Reconnecting the session here would not 
be enough: the original entities would have to be still available (kept in 
the service memory) and synchronized by the service code with the modified 
ones.

On Sunday, June 1, 2025 at 7:23:28 PM UTC+2 Michael W Powell wrote:

> Hello,
>
> I intend to hopefully operate with detached instances, feeding WPF, view 
> models, views, etc. And only gin up a new session (repository pattern) 
> instance when it comes time to save again.
>
> In doing that, I can attach the instances to the session again? Does NH do 
> required differencing, of individual instances gauging the upsert 
> requirement, as well as reconciling then would be ophaned instances?
>
> Coming from EF, I was finding I had to do some manual deletion of 
> orphaning records. Otherwise, would find that duplicate or multiple old 
> records would come back when the query was refreshed. This I want to 
> obviously avoid.
>
> (F)NH advise appreciated.
>
> Thanks!
>
> Best,
>
> Michael W. Powell
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/nhusers/796ed790-5856-4952-af74-7b14f36b3d72n%40googlegroups.com.

Reply via email to