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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:44:06+00:00 2026-05-24T05:44:06+00:00

I ran into a strange NHibernate and Automapper problem. I am not sure which

  • 0

I ran into a strange NHibernate and Automapper problem. I am not sure which one is to blame but I am struggling for a whole day now and I can’t seem to find out why.

Here is my Nhibernate mapping files:

Navigation.hbm.xml

<id name="ID" column="NavigationID">
  <generator class="identity"></generator>
</id>

<property name="IsDefault"/>
<property name="RoleType" column="RoleTypeID" />

<bag  name="Items" cascade="save-update" inverse="true" lazy="false" fetch="join">
  <key column="NavigationID"/>
  <one-to-many class="NavigationItem"/>
</bag>

NavigationItem.hbm.xml

 <class name="NavigationItem" table="NavigationItem">

<id name="ID" column="NavigationItemID">
  <generator class="identity"></generator>
</id>

<property name="ShowInMenu"/>
<property name="Order" column="[Order]" />

<many-to-one name="Page" column="PageID" lazy="false" fetch="join" />
<many-to-one name="Navigation" column="NavigationID" />
<many-to-one name="Parent" column="ParentNavigationItemID" />

<bag  name="Items" cascade="save-update" inverse="true">
  <key column="ParentNavigationItemID"/>
  <one-to-many class="NavigationItem"/>
</bag>

This is how I fill up a Navigation object:

ISession session = SessionProvider.Instance.CurrentSession;

            using (transaction = session.BeginTransaction())
            {
                var navigation = session.QueryOver<Navigation>()
                    .Where(x => x.IsDefault && x.RoleType == null)
                    .TransformUsing(new NHibernate.Transform.RootEntityResultTransformer())
                    .SingleOrDefault();

                transaction.Commit();
                return navigation;
            }

Since the Items bag on the Navigation object is set to lazy=”false”, I get only one query to the database to get the Navigation object and a left join to get all the Navigation items as well.

All is perfect until now.

I did a test to iterate through all the items and the sub-items recursive and no more hits to the database.

Then, I have an UI model that I map with Automapper.

Here are the UI models:

public class NavigationModel
{
    public List<NavigationItemModel> Items { get; set; }

    public NavigationModel()
    {
        Items = new List<NavigationItemModel>();
    }
}

public class NavigationItemModel
{
    public string PageName { get; set; }
    public string Url { get; set; }
    public bool Selected { get; set; }

    public NavigationItemModel Parent { get; set; }
    public List<NavigationItemModel> Items { get; set; }
}

And the automapper mappings:

AutoMapper.Mapper
            .CreateMap<NavigationItem, NavigationItemModel>()
 // IF I REMOVE THE NEXT LINE, IT HITS THE DATABASE FOR EACH SUB-ITEM of the NavigationItem.Items
            .ForMember(m => m.Items, o => o.Ignore()); 

        AutoMapper.Mapper
            .CreateMap<Navigation, NavigationModel>();

Ok, now the behavior is like this:

  • If I ignore the NavigationItem.Items member in the mapping, all goes well, but only the Navigation and it’s items are mapped. No sub-items collection of the navigation’s Items are mapped. BUT the database is not hit anymore. But I want the other items mapped as well…
  • If I remove the line under the comment, the database is hit for each of the Navigation.Items, querying for it’s sub-items (where ParentID = Item.ID).

Any idea what am I doing wrong?

Sorry for the wall of text, but I thought better to describe it in more detail, I spent the whole day on this one and I tried all kind of queries with Future and JoinQueryOver, etc. The problem does not seem to be with NHibernate since that loads fine and I can iterate without any more calls to the database.


I forgot to include the SQL that is being generated:

First there is this query:

SELECT this_.NavigationID             as Navigati1_7_2_,
   this_.IsDefault                as IsDefault7_2_,
   this_.RoleTypeID               as RoleTypeID7_2_,
   items2_.NavigationID           as Navigati5_4_,
   items2_.NavigationItemID       as Navigati1_4_,
   items2_.NavigationItemID       as Navigati1_4_0_,
   items2_.ShowInMenu             as ShowInMenu4_0_,
   items2_.[Order]                as column3_4_0_,
   items2_.PageID                 as PageID4_0_,
   items2_.NavigationID           as Navigati5_4_0_,
   items2_.ParentNavigationItemID as ParentNa6_4_0_,
   page3_.PageID                  as PageID8_1_,
   page3_.Name                    as Name8_1_,
   page3_.Title                   as Title8_1_,
   page3_.Description             as Descript4_8_1_,
   page3_.URL                     as URL8_1_
FROM   Navigation this_
   left outer join NavigationItem items2_
     on this_.NavigationID = items2_.NavigationID
   left outer join Page page3_
     on items2_.PageID = page3_.PageID
WHERE  (this_.IsDefault = 1 /* @p0 */
    and this_.RoleTypeID is null)

Then, when Automapper comes into play, a list of these queries are being generated, only the p0 parameter differs (from 1 to 12 … the number of items without parents )

SELECT items0_.ParentNavigationItemID as ParentNa6_2_,
   items0_.NavigationItemID       as Navigati1_2_,
   items0_.NavigationItemID       as Navigati1_4_1_,
   items0_.ShowInMenu             as ShowInMenu4_1_,
   items0_.[Order]                as column3_4_1_,
   items0_.PageID                 as PageID4_1_,
   items0_.NavigationID           as Navigati5_4_1_,
   items0_.ParentNavigationItemID as ParentNa6_4_1_,
   page1_.PageID                  as PageID8_0_,
   page1_.Name                    as Name8_0_,
   page1_.Title                   as Title8_0_,
   page1_.Description             as Descript4_8_0_,
   page1_.URL                     as URL8_0_
FROM   NavigationItem items0_
   left outer join Page page1_
     on items0_.PageID = page1_.PageID
WHERE  items0_.ParentNavigationItemID = 1 /* @p0 */

This is taken from the NHProf application, hope it helps.

Thank you,
Cosmin

  • 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-24T05:44:06+00:00Added an answer on May 24, 2026 at 5:44 am

    I think AutoMapper is mapping your classes recursively. If this is the case, than you can specifiy the max depth for your mappings using

    Mapper.CreateMap<TSource, TDestination>().MaxDepth(2); // or 1, or 3, or whatever
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started with opengl but I ran into some strange behaviour. Below I
I ran into the problem that my primary key sequence is not in sync
I've ran into a problem while trying to test following IRepository based on NHibernate:
Ran into a strange problem in PHP today and I'm wondering if someone can
I ran into a strange problem. I use DIV as a container, and put
I ran into a strange problem. In my unit test, I want to check
I ran into a strange problem using a C# webservice client to call a
I ran into a situation today where Java was not invoking the method I
I ran into a strange issue over the weekend while I was working on
I ran into a strange issue with my indexed tableview's section index titles. The

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.