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 1096077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:13:40+00:00 2026-05-17T00:13:40+00:00

I have an application using Fluent NHibernate on the server side to configure the

  • 0

I have an application using Fluent NHibernate on the server side to configure the database. Fluent uses Lazyloading as default, but I explicitly disabled this as this gave me problems when sending the objects to the client side. Obviously the client can’t load the objects lazily as it doesn’t have access to the database.

Now I try reenabling Lazyloading for parts of my datamodel as there are some parts where I only want to return toplevel objects to the client. However, they don’t seem to be Lazyloaded. Why?!

What I did to disable LazyLoading was adding Not.LazyLoading() in the mapping object, and on references in the mapping. Now removing this doesn’t seem to have effect. Debugging I see all the referenced objects, and I also get them all on the client side. However, the NHibernateUtil.IsInitialized(myObjectFromDb.SomeReference) correctly says false at the same time. So; how do I ensure that the objects are lazy-loaded; getting a object missing its references back to the client? Any ideas what I might got wrong?

I have a few classes (very simplified example..):

public class Customer
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Order> Orders { get; set; }
}

public class Order
{
    public virtual int Id { get; set; }
    public virtual IList<Item> Items { get; set; }
}

public class Item
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

Simple mappings – using default LazyLoading:

public class CustomerMapping : ClassMap<Customer>
{
    public CustomerMapping()
    {
        Id(c => c.Id);
        Map(c => c.Name);
        HasMany(c => c.Orders);
    }
}

public class OrderMapping : ClassMap<Order>
{
    public OrderMapping()
    {
        Id(c => c.Id);
        HasMany(c => c.Items);
    }
}

public class ItemMapping : ClassMap<Item>
{
    public ItemMapping()
    {
        Id(c => c.Id);
        Map(c => c.Name);
    }
}

And I fetch it straight forward with a Session.Load<Customer>(id) – returning the result over my REST service directly without accessing the object such that the lazy references are loaded. Both right after the Load and on the object returned to the server side the references are loaded. How can I prevent this?

  • 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-17T00:13:41+00:00Added an answer on May 17, 2026 at 12:13 am

    Did some more research and testing. Lazy Loading turns out to be as simple as expected. This is the default behavior in the mappings, so not saying anything about it will actually cause the objects to be Lazy Loaded.

    The question is: When are they loaded? Well – if they are Lazy Loaded they will be loaded whenever anyone needs them – within the scope of the session. So; inspecting the object in Debug-mode to check if it is loaded will in fact trigger loading of the object – lazily. So the inspector will show the references as loaded, and everything will look fine.

    The problem in my case was what I mentioned about “within the session scope”. I have a client and server side. The server provides REST services, and the client calls these using an HttpClient. On the server the data will be wrapped in XML, and then returned. In my case the session still lives when I return the result – meaning that the wrapping of the data will load the references lazily – when needed. So I get all the references, and the complete object is returned to the client.

    So; to ensure Lazy Loading with Fluent NHibernate just don’t set anything explicitly, and then be aware if you actually use the objects references in any way as this will cause them to be loaded.

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

Sidebar

Related Questions

No related questions found

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.