Nhibernate Lazy Load exception after a view exception

前端 未结 2 1586
执念已碎
执念已碎 2021-01-21 21:35

I get a weird behavior with NHibernate with Fluent Configuration.

Whenever a generic exception unrelated to the NHibernate occurs i.e. in the view a DivideByZeroEx

2条回答
  •  盖世英雄少女心
    2021-01-21 22:20

    As Radim Köhler noted i was saving a lazy-loaded object in Session that caused the problem.

    But i wanted to avoid the Serilization of all objects and i fixed it as follows.

    I added the following method to eager-load an entity instead of lazy

    public T FindByEager(int id)
        {
            T entity = FindBy(id);
            NHibernateUtil.Initialize(entity);
            return entity;
        }
    

    And changed BaseController to

    if (Session != null) Session["User"] = userRepository.FindByEager(LoggedUser.Id);
    

提交回复
热议问题