I'm currently learning NHibernate and I would like to d开发者_运维百科ata-bind to Web controls (i.e. GridView).
In my current example I am using Fluent NHibernate to map two tables to their business objects (Project and ProjectStatus). I also have a "Project has a ProjectStatus" (many-to-one) relationship.
Structure of Project class:
Project.ID
Project.Name
Project.ProjectStatus.Name
Project.ProjectStatus.Description
When I bind a list of Project objects (with lazy="proxy") to a GridView, the GridView does not trigger the load of the referenced table. As a result the fields Name and Description of ProjectStatus are left out by the GridView control.
Is there a way of having true lazy loading in combination with a data-bound GridView?
Thanks in advance.
This should work without any problems. I suspect the problem is in the data binding itself not accessing the properties of a child object. How are you binding to the ProjectStatus properties? As far as I know, you can't directly bind to child objects. You have to set values in an OnRowDataBound event handler or by using Eval.
I would first create an OnRowDataBound handler and break on a DataRow. Examine e.Row.DataItem in the immediate window to verify that the child object is being loaded. If it is, then the problem lies with the binding.
Are you storing your ISession in the request context and flushing it at the end of the request? If not, you should start a new session in the begin_ request method of the global.asax.cs, store it in request context, then grab it back in the end_request method, and flush it.
精彩评论