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.

Reply via email to