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] <javascript:_e({}, 'cvml',
> 'nhusers%[email protected]');>.
> To post to this group, send email to
> [email protected]<javascript:_e({}, 'cvml',
> '[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.