It seems that altough I have been using NHibernate for a while now, I still misunderstand some basic concepts of this ORM. Let’s say I have a class called “Blog” and I load a persisted instance like so:
using (var tx = Session.BeginTransaction())
{
var myBlog = Session.Get(10);
tx.Commit();
}
If I now change a property of this instance, NHibernate seems to automatically detect the unsaved changes and will produce an UPDATE on transaction-commit.
This causes, that the following statements do exactly the same:
using (var tx = Session.BeginTransaction())
{
var myBlog = Session.Get(10);
myBlog.Title = "Changed title";
tx.Commit();
}
using (var tx = Session.BeginTransaction())
{
var myBlog = Session.Get(10);
myBlog.Title = "Changed title";
Session.Update(myBlog); // why is this necessary?
tx.Commit();
}
I don’t see any difference with NHProf. So why does the explicit Update-method exists and when should I use it?
Entities are not always connected with session. For example you could have webservice with method, that accepts some entity, and updates in db:
This code executes update in database without issuing select.