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

The Archive Base Latest Questions

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

Given this: namespace TheEntities { [DataContract(IsReference=true)] public class Question { [DataMember] public virtual int

  • 0

Given this:

namespace TheEntities
{
    [DataContract(IsReference=true)]    
    public class Question
    {
        [DataMember] public virtual int QuestionId { get; set; }
        [DataMember] public virtual string Text { get; set; }
        [DataMember] public virtual string Poster { get; set; }

        [DataMember] public virtual IList<QuestionComment> Comments { get; set; }
        [DataMember] public virtual IList<Answer> Answers{ get; set; }



        [DataMember] public virtual byte[] RowVersion { get; set; }
    }

    [DataContract]
    public class QuestionComment
    {
        [DataMember] public virtual Question Question { get; set; }        

        [DataMember] public virtual int QuestionCommentId { get; set; }
        [DataMember] public virtual string Text { get; set; }
        [DataMember] public virtual string Poster { get; set; }
    }


    [DataContract(IsReference = true)]
    public class Answer
    {
        [DataMember] public virtual Question Question { get; set; }

        [DataMember] public virtual int AnswerId { get; set; }
        [DataMember] public virtual string Text { get; set; }
        [DataMember] public virtual string Poster { get; set; }

        [DataMember] public virtual IList<AnswerComment> Comments { get; set; }

    }

    [DataContract]
    public class AnswerComment
    {
        [DataMember] public virtual Answer Answer { get; set; }

        [DataMember] public virtual int AnswerCommentId { get; set; }
        [DataMember] public virtual string Text { get; set; }
        [DataMember] public virtual string Poster { get; set; }
    }

}

Entity Framework didn’t produce duplicate objects for Answer, QuestionComment, AnswerComment while NHibernate does.

public Question OpenQuestion(int id)
{
    var repo = QuestionRepository;

    var query = repo.All.Where(y => y.QuestionId == id);

    if (QuestionRepository.GetType() == typeof(EfRepository<Question>))
    {                
        query = query
                .Include("Answers")
                    .Include("Answers.Comments")
                .Include("Comments");

        return query.Single();
    }
    else if (QuestionRepository.GetType() == typeof(NhRepository<Question>))
    {                
        // kinda sad, produces duplicate objects
        query = query
                .FetchMany(x => x.Answers)
                    .ThenFetchMany(x => x.Comments)
                .FetchMany(x => x.Comments);
    }
    else
        throw new Exception("Something unsupported");

    return query.Single();
}

This also produces duplicate objects (three levels deep, using three relations):

query = query
    .FetchMany(x => x.Answers)
    .ThenFetchMany(x => x.Comments)

This also produces duplicate objects(two levels deep only, but using three relations):

query = query
    .FetchMany(x => x.Answers)
    .FetchMany(x => x.Comments);

This doesn’t produces duplicate objects, however the eager loading is for two levels deep only and two relations, i.e. from Question to Answer. For comments to question, and comments to answer, they are performed on separate query.

query = query
    .FetchMany(x => x.Answers);

If NHibernate can only do its job well for two levels FetchMany only with two relations only, why bother creating ThenFetchMany(used on three levels, but has error, has duplicate objects)? In fact, even FetchMany is useless too if you want to use it on three relations also, it produces duplicate objects too.

Can NHibernate team be bothered to remove ThenFetchMany anyway since it can’t work properly?

There’s no error in my mapping, things are working properly (i.e. doesn’t produce duplicate objects) when I removed the fetching strategies.

  • 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-24T12:44:00+00:00Added an answer on May 24, 2026 at 12:44 pm

    to get unique results do

    for Linq:

    .Distinct()
    

    for QueryOver

    .TrasformUsing(Transformers.DistinctRootentity)
    

    for Criteria

    .SetResulttransformer(Transformers.DistinctRootentity)
    

    EDIT: effectivly this is a shortcoming of NH, it will issue a cartesian product, but this can be improved

    repo.All.Where(y => y.QuestionId == id)
            .FetchMany(x => x.Answers)
                .ThenFetchMany(x => x.Comments)
            .Future()
    
    query = repo.All.Where(y => y.QuestionId == id)
            .FetchMany(x => x.Comments)
    
    var result = query.AsEnumerable().Single();
    

    see http://ayende.com/blog/4367/eagerly-loading-entity-associations-efficiently-with-nhibernate

    it looks a bit weird but should do

    • 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.