The problem seems to be that the entity outlives the session, that is, you
seem to be storing the entity somewhere along one request, and retrieving
it in another request. On this second request, the session was already
disposed, so the entity cannot use it to load the lazy properties. What you
should do is 1) force the loading of all required properties prior to
storing the entity or 2) attaching the entity to a session in the second
request.
RP
On Wednesday, September 11, 2013 4:26:05 PM UTC+1, fknebels wrote:
>
> Where are you making this call to someUser.Roles? Is it in a view of an
> MVC application? You you are trying to access someUser.Roles in the view
> then the session has already been closed and you are out of luck. Are you
> using view models in your MVC applications. You could grab whatever you
> need from the session on the controller, populate a view model and return
> everything you need for the view.
>
> What is you session strategy? Are you creating and disposing of a session
> every web request?
>
> On Wednesday, September 11, 2013, wrote:
>
>> I have only 2 entities, User and Role, where User have a Roles property,
>> and Role a Users property.
>>
>> I'm sharing this library between a few projects, Asp.Net MVC and Asp.Net
>> Web Api.
>>
>> However, when for instance I do a "someuser.Roles", I get the following
>> error:
>>
>> Error retrieving roles for user -
>>> Initializing[Toolbox.Security.Models.User#1]-failed to lazily initialize a
>>> collection of role: Toolbox.Security.Models.User.Roles, no session or
>>> session was closed
>>
>>
>> When I change my configuration to the following all works fine, but I
>> really need lazy loading to work, otherwise getting a user will retrieve
>> all his roles and all the users in that roles. Thousands of unnecessary
>> database calls
>>
>> public class SecurityUserMapping : ClassMapping<User>
>>> {
>>> public SecurityUserMapping()
>>> {
>>> Table("Users");
>>> Id(x => x.Id, m => m.Generator(Generators.Identity));
>>> Property(x => x.Title);
>>> // etc, etc
>>> Set(x => x.Roles, c =>
>>> {
>>> c.Key(k =>
>>> {
>>> k.Column("UserId");
>>> k.NotNullable(true);
>>> });
>>> c.Table("Users_Roles");
>>> c.Lazy(CollectionLazy.NoLazy); // must be LAZY!
>>> }, r => r.ManyToMany(m => m.Column("RoleId")));
>>> }
>>> }
>>
>>
>> For what it matter my configuration look as follows:
>>
>> private static void InitializeSessionFactory()
>> {
>> var mapper = new ModelMapper();
>> mapper.AddMappings(Assembly.GetAssembly(typeof
>> (SecurityUserMapping)).GetExportedTypes());
>> HbmMapping domainMapping =
>> mapper.CompileMappingForAllExplicitlyAddedEntities();
>> var cfg = new Configuration();
>> cfg.Proxy(p =>
>> p.ProxyFactoryFactory<DefaultProxyFactoryFactory>())
>> .DataBaseIntegration(db =>
>> {
>> db.ConnectionStringName =
>> "TooboxSecurityConnectionString";
>> db.Dialect<MsSql2008Dialect>();
>> db.BatchSize = 500;
>> })
>> .AddAssembly(typeof (SecurityUserMapping).Assembly)
>> .AddAssembly(typeof (SecurityRoleMapping).Assembly)
>> .SessionFactory().GenerateStatistics();
>> cfg.AddMapping(domainMapping);
>> _sessionFactory = cfg.BuildSessionFactory();
>> }
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "nhusers" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/nhusers.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/groups/opt_out.