Fluent NHibernate and lazy load
10,272
I found an answer of my question.
If don't write anywhere .Not.LazyLoad()
and need to get Comments
, you must to write this (get user with id=1):
var user = session.QueryOver<User>()
.Fetch(u => u.Comments)
.Eager
.List()
.Where(u => u.Id == userId)
.FirstOrDefault();
Or, what a you need.
Related videos on Youtube
Comments
-
LuckSound about 2 years
I have some questions about about lazy loading
When I have mapped my objects, I write
.Not.LazyLoad()
everywhere in my application and it works good. But I have some problems.
Example: I have a classUser
. It has propertiesName
andComments
. MappingComments
inUser
:HasMany(x => x.Comments).KeyColumn("UserId").Not.LazyLoad();
Which works good, but everywhere I load
User
,Comments
get loaded with it, which is bad... Example of loadUser
:var user = session.Get<User>(1);
If the user has a lot of comments my application works bad...
The question is how do I enable LazyLoad if needed? Or how do I disable Lazy loading, if I don't write.Not.LazyLoad()
? -
aggietech over 9 yearsThe reason is - if something is lazy loaded ... you'll need to eager fetch it when you want it.