Use fluent nhibernate to retrieve parent object
I have a one to many mapping. When I try to retrieve the parent object the child object also comes up even if I stop lazy loading in mapping. I want to get onl开发者_运维问答y the parent object and not the child ones.How can I do?
You are apparently triggering the lazy loading when you debug through your code.
If you are not sure what this means then please try to understand this:-
- You put a breakpoint on your code
- You retrieve the parent
- Visual studio hits the breakpoint
- You look to see if the children has been loaded
- The visual studio debugger goes and retrieves the children for you <- This is what is causing your lazy loading. By observing this in VS you are forcing a lazy load.
If you are still not sure then please either add a SQL profiler, use logging via log4net or even download the NHProf (its free for 30 days) and see the generated sql with and then without the breakpoint and you will see the differences.
HTH
why you access the childs if you dont need them? lazy collections aren't initialized when you dont access them. Maybe something like this in postloading event:
var pc = Parent.Childs as IPersistentCollection;
if (pc != null && !pc.WasInitialized)
Parent.Childs = null;
精彩评论