Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4016434
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:44:22+00:00 2026-05-20T09:44:22+00:00

Using lazy=true in my classes causes my application to work perfectly, but the performance

  • 0

Using lazy=”true” in my classes causes my application to work perfectly, but the performance is horrible. I turned this on back when I was creating this from a tutorial and just wanted to get something working as quickly as possible. (I used this tutorial: http://geekswithblogs.net/BobPalmer/archive/2010/04/23/mapping-object-relationships—quickstart-with-nhibernate-part-3.aspx which was very helpful at getting something that worked quickly)

I don’t need it to load all of these many-to-one classes when I just need to use the one object, so it made sense to turn lazy loading back on. Then, I looked into the objects and saw nothing but exceptions for those many-to-one classes inside my main objects. When I try to use those properties later I get the following error:

"Could not initialize proxy - no Session." 

I’m guessing this means that the session is closed, so it fails when trying to lazy-load the additional objects. My session provider looks like this (same as the tutorial):

class SessionProvider {
    private static ISessionFactory _sessionFactory;
    private static Configuration _config;

    public static ISessionFactory SessionFactory {
        get {
            if (_sessionFactory == null) {
                _sessionFactory = Config.BuildSessionFactory();
            }
            return _sessionFactory;
        }
    }

    private static Configuration Config {
        get {
            if (_config == null) {
                _config = new Configuration();
                _config.AddAssembly(Assembly.GetCallingAssembly());
            }
            return _config;
        }
    }
}

Which is then used by my repositories like this:

using (var session = GetSession()) { ... }

Which gets the session from this function:

private static ISession GetSession() {
        return SessionProvider.SessionFactory.OpenSession();
}

So my question is, what am I expected to do here? Keep the session open? Make it static across all repositories? I don’t have enough experience with NHibernate to understand how this works yet. My priority right now is only reading from the database, if that makes any difference. This is going in a code library that will eventually be used both on our website and various C# .Net apps.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:44:22+00:00Added an answer on May 20, 2026 at 9:44 am

    The recommended approach is to use the unit of work pattern. Another option for you would be to use eager loading if you know you’re going to need specific entities up front.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
I'm using .net 2.0 with NHibernate/ActiveRecord and WCF. I haven't been using NH Lazy
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
Using JDeveloper , I started developing a set of web pages for a project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.