2011/4/26 PLen <[email protected]>:
[...]
>
> What I found was that if I didn't use the SAME ISession (the same
> session that was used to retrieve the objects) when I went to do an
> Update call, I got an exception that the session had been closed and
> the update could not take place.  I could only do a successful update
> on an object if I used the same session.  Per your last sentence, how
> do you persist changes to the DB if you don't call Update() on the
> changed object?
>

The error about session closed seems weird. Without having seen the
exact code, I honestly suspect that the session simply was closed. The
two examples in "pseudocode" below should work. This is covered by
section 9.4 and 9.6 in the reference. 9.7 might be applicable also.


Performing changes inside a single session. For many applications this
is the only method needed:

using (sess1 = OpenSession())
using (trans = sess1.BeginTransaction())
{
    fooInstance = sess1.Get<Foo>(4);
    fooInstance.SomeProperty = "new value";
    // Using default flush mode in the session, this will cause any
changes in any
    // object that sess1 knows about to be sent to DB:
    trans.Commit();
}



If you must detach objects from the loading session, you can use a
second transaction
to commit the changes:

using (sess1 = OpenSession())
using (trans = sess1.BeginTransaction())
{
    fooInstance = sess1.Get<Foo>(4);
}

// sess1 have now been closed and disposed.

// fooInstance now does not belong to any session.
// we can make changes in the object of course.
fooInstance.SomeProperty = "new value";

// to persist the change, we need to use a new session.
using (sess2 = OpenSession())
using (trans = sess2.BeginTransaction())
{
    // Make sess2 aware of the object:
    sess2.Update(fooInstance);

    // Using default flush mode in the session, this will cause any
changes in any
    // object that sess2 knows about to be sent to DB:
    trans.Commit();
}


/Oskar

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to