views:

67

answers:

0

I'm starting to play with NHibernate to make a case for its use in our company's projects. I've created a branch of one of our solutions and have started to convert some of the data access to using NHibernate as a proof-of-concept. However, while looking at SQL Profiler, I discovered that NHibernate is issuing an update command to the database when I databind an IList that I get back from a Criteria query to a Datalist control. The Criteria query looks like this:

 var contacts = session.Get<Location>(LocationId).Contacts
                .OrderBy(x => x.LastName)
                .ThenBy(x => x.FirstName)
                .ThenBy(x => x.ContactType.TypeName)
                .ThenBy(x => x.ContactID);
 dlContact.DataSource = contacts;
 dlContact.DataBind();

I'm getting the list of contacts that are associated with a particular location in our system. To be clear, the update does not seem to be issued right as DataBind() is called, but when the session is flushed at the end of the request. It issues an update to the Contacts table, not the Location table. And it's a single update, too, not one update for each contact associated with the location.

Why does NHibernate think it needs to update the database? Is the databinding changing the value of one of the Contact entities returned from the query somehow? I'm just displaying the values on the page, not changing them anywhere that I can tell, so I don't think changetracking should have caught anything.

I suppose I could fix this by not binding directly to the collection I get back from NH, instead doing a projection over the collection to just get back a list of anonymous types, sort of a just-in-time ViewModel, but I'd rather not do that if I don't have to.

Is there a way I could possibly turn off changetracking for this result set? That might fix it, too.

Any help would be much appreciated. I can provide more code context if it would be helpful.