c# – Error returning list with Fluent NHibernate


Inside the Web.API project, I created a Get() method which returns a list of objects – IList. This AccessPerson class has a Log list (1:N).

When executing the Get() method, an exception occurs:

<ExceptionMessage>Initializing[RemyWebModel.Entidades.PessoaAcesso#1]-failed to lazily initialize a collection of role: RemyWebModel.Entidades.PessoaAcesso.Logs, no session or session was closed</ExceptionMessage>

Método Get():

    public HttpResponseMessage Getall()
        IList<PessoaAcesso> pessoasAcesso;

        using (var session = NHSessionFactoryManager.GetSessionFactory().OpenSession())
            pessoasAcesso = session.QueryOver<PessoaAcesso>().List();

        return Request.CreateResponse(HttpStatusCode.OK, pessoasAcesso, new JsonMediaTypeFormatter());

Person Class Access:

public class PessoaAcesso : Pessoa
    [Display(Name = "Login de acesso")]
    [RegularExpression(@"[a-zA-Z]{5,15}", ErrorMessage = "Login deve possuir somente letras e conter entre 5 a 15 caractéres!")]
    [Required(ErrorMessage = "O login deve ser preenchido!")]
    public virtual string Login { get; set; }

    [Display(Name = "Senha de acesso")]
    [Required(ErrorMessage = "A senha deve ser informada!")]
    public virtual string Senha { get; set; }

    [Display(Name = "Cofirme novamente a senha")]
    [Compare("Senha", ErrorMessage = "As senhas não conferem!")]
    public virtual string ConfirmarSenha { get; set; }

    [Display(Name = "Acesso Administrador?")]
    public virtual bool Administrador { get; set; }

    [Display(Name = "Acesso liberado?")]
    public virtual bool AcessoLiberado { get; set; }

    [Display(Name = "Perfil")]
    public virtual PermissaoPerfil Perfil { get; set; }

    public virtual IList<Log> Logs { get; set; }

    public PessoaAcesso()
        Logs = new List<Log>();

Log class:

public class Log
    public virtual int Id { get; protected set; }

    public virtual PessoaAcesso PessoaAcesso { get; set; }

    [Display(Name = "Data/Hora")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy - hh:mm:ss}")]
    public virtual DateTime DataHora { get; set; }

    [Display(Name = "Ação")]
    public virtual LogAcao Acao { get; set; }

    [Display(Name = "Descrição")]
    [Required(ErrorMessage = "Descrição precisa ser preenchida!")]
    public virtual string Descricao { get; set; }

PersonAccess Mapping:

public class PessoaAcessoMap : SubclassMap<PessoaAcesso>
    public PessoaAcessoMap()

        Map(x => x.Login)

        Map(x => x.Senha)

        Map(x => x.Administrador)

        Map(x => x.AcessoLiberado)

        References(x => x.Perfil)

        HasMany(x => x.Logs)

As I understand it couldn't load the children "Log" because the session is closed, I just don't understand where I'm going wrong. Why does this error occur?


The problem was in the session block:

using (var session = NHSessionFactoryManager.GetSessionFactory().OpenSession())

In this case, I need to leave the session open so that the result can be completely delivered, in the case of child records – Log.

Therefore, I removed the use of using leaving the session variable open:

public IEnumerable<PessoaAcesso> GetAll()
        var session = NHSessionFactoryManager.GetSessionFactory().OpenSession();

        return session
Scroll to Top