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

  • Home
  • SEARCH
  • 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 7596069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:49:46+00:00 2026-05-30T21:49:46+00:00

I have been putting off this question for some time but now it is

  • 0

I have been putting off this question for some time but now it is bothering me all the time. I have 2 classes (These classes are automapped – I leave out public and virtual for brevity.):

class Cashier
{
    int Id {get; set;}
    string name{get; set;}
    IList<Site> Sites {get; set;}
}

class Site
{
    int Id{get; set;}
    string name {get; set;}
    Cashier Cashier {get; set;}
}

I load cashiers with this :

sealed class Repository<T> : IRepository<T> where T :class
    {
        public IList<T> Items { get; private set; }

        public void Load()
        {
            Items.Clear();
            var session = SessionHelper.GetSession();
            session.Clear();
            using (var tx = session.BeginTransaction())
            {
                var list = session.Query<T>().ToList();

                foreach (var obj in list)
                {
                    Items.Add((T)session.Merge(obj));
                }

                session.Clear();
                tx.Commit();
            }
        }
//more
}

and in a winform i bind it to a bindingsource like this:

cashierBindingSource.DataSource = Cashiers;

I set this bindingsource as the data source of a combobox and when I run the application and click on the combobox this exception is thrown at me :

Initializing[HRProject.Model.Cashier#1]-failed to lazily initialize a collection of role: HRProject.Model.Cashier.Sites, no session or session was closed

I found that providing a mapping override fixes this problem

mapping.HasMany(x => x.Sites).Not.LazyLoad().Cascade.All();

However, I have many such classes and I get the feeling there’s a cleaner way to get this done. Any ideas?

  • 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-30T21:49:47+00:00Added an answer on May 30, 2026 at 9:49 pm

    In my experience, FNH, WinForms data binding, and lazy loading are a difficult mix.

    If you need lazy loading, you need to keep a session open for the life of the form.

    If you don’t need it, you can turn it off.

    The easiest way I’ve found to do this for a whole project is to use the DefaultLazy convention:

    .Conventions.Add( DefaultLazy.Never() )
    

    Go to “The Simplest Conventions” section on the wiki, for this, and a list of others.

    Here’s the list from the Wiki:

    Table.Is(x => x.EntityType.Name + "Table")
    PrimaryKey.Name.Is(x => "ID")
    AutoImport.Never()
    DefaultAccess.Field()
    DefaultCascade.All()
    DefaultLazy.Always()
    DynamicInsert.AlwaysTrue()
    DynamicUpdate.AlwaysTrue()
    OptimisticLock.Is(x => x.Dirty())
    Cache.Is(x => x.AsReadOnly())
    ForeignKey.EndsWith("ID")
    

    A word of warning – some of the method names in the Wiki may be wrong. I edited the Wiki with what I could verify (i.e. DefaultCascade and DefaultLazy), but can’t vouch for the rest. But you should be able to figure out the proper names with Intellisense if the need arises.

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

Sidebar

Related Questions

I have been putting off developing this part of my app for sometime purely
I have been working on this script for a long time, but the code
I am putting together some ideas for our automated testing platform and have been
Have been studying the file system related classes of Adobe AIR 1.5, but so
I have been putting off activity on SO because my current reputation is 1337.
I have been trying to work out the bug on this but can't seem
I have been putting only jQuery mask so far, but I figured out that
I am stuck with this, I have been putting myself through a crash course
I have been putting some data I have into my database. One of the
I have been putting them in the toplevel. I am wondering if it is

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.