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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:27:03+00:00 2026-06-14T11:27:03+00:00

I was coding a very simple case with 2 classes: Parent and Child. Parent

  • 0

I was coding a very simple case with 2 classes: Parent and Child. Parent has n children, and children has 1 Parent. I set up a bidirectional relationship between them.

I was trying to add a business rule to my parent, that rule checked for equality between the child’s parent and the instance handling the call. It returned false when it should have returned true. So I simplified everything to get to the root of the problem. So I tested the same equality outside the POCO and it returned true:

Parent parent0 = session.Load<Parent>(0);
Child child = session.Load<Child>(0);

bool externalTest = parent0 == child.Parent;

I then coded a method for my Parent to test the exact same thing:

bool internalTest = parent0.IsRelated(child);

... Parent Class code

public virtual bool IsRelated(Child child)
{
    return child.Parent == this;            
}
...

And it returns false… I just don’t get it. It’s the exact same code.

More info:

So to get more info, I modified my test:

Parent parent0 = session.Load<Parent>(0);
Child child = session.Load<Child>(0);

bool externalTest = parent0 == child.Parent;
System.Diagnostics.Debug.WriteLine("outside parent: " + externalTest);
System.Diagnostics.Debug.WriteLine("Number of parent instances before call to IsRelated:" + Parent.NumberOfInstances);
parent0.IsRelated(child, parent0);
System.Diagnostics.Debug.WriteLine("Number of parent instances after call to IsRelated:" + Parent.NumberOfInstances);

... Parent Class code
public virtual void IsRelated(Child child, Parent sameAsThis)
{
    bool internalTest = child.Parent == this;
    System.Diagnostics.Debug.WriteLine("inside parent:" + internalTest);

    bool sameTest = sameAsThis == this;
    System.Diagnostics.Debug.WriteLine("this should equal sameAsThis:" + sameTest);

}
...

I passed the parent instance directly to itself and verify it was the same instance. Well it’s not, I get another instance created when I enter the IsRelatedMethod.

Here are my test results:

outside parent: True

Number of parent instances before call to IsRelated:1

inside parent:False

this should equal sameAsThis:False

Number of parent instances after call to IsRelated:2

What am I doing wrong ?

For detailed mapping files and pocos, see (http://stackoverflow.com/questions/13253459/relationships-fixup-in-entityframework-vs-nhibernate)

  • 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-06-14T11:27:05+00:00Added an answer on June 14, 2026 at 11:27 am

    This is because of the proxy that Nhibernate uses to lazy load entities. In your case the child’s parent is a proxy instance.

    To solve you problem simply change .Load to .Get

    .load does not actually hit the database and populate the entity. See this blog post by Ayende for more info

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

Sidebar

Related Questions

I had a frustrating problem recently that boiled down to a very simple coding
Sorry for asking very simple question.I'm new to coding. Input txt file 5,3 001
I am trying to build a very simple price comparison script. Until now, I
i am trying to make a very simple game where i have 7 positions
While coding very simple program for removing blanks, tabs, newlines I came across something
I am quiet a newbie in coding, so the answer is maybe very simple.
Greetings all, I'm trying to create a specific layout that should be very simple
I am trying to do something very simple: update a few labels on my
I know this is very simple question, but I'm apparently terrible at coding and
I'm looking at coding up a very simple server using an ssl connection -

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.