tags:

views:

53

answers:

2
Father father = BL.GetFatherById(1);  
Product product = BL.GetByID(123);  

(father.Products[553] == product)  

product.delete = true;  

father.Products[553].delete == false !!??   

why is that ?

aren't they connected ?? its the same object.

+3  A: 

As you can read in section 10.3 of the NHibernate reference manual database identity and CLR object identity are equivalent per session.

Therefore Object.ReferenceEquals(foo, bar) will yield true if and only if foo and bar are attached to the same session and map to the same database row. Be careful when using == for comparing object identity - the operator may have been overloaded (but you should usually know that).

In consequence you should always get the same object no matter what query you use for the object as long as you stay within the same session. Are you using multiple sessions? Maybe a Unit of Work pattern and you are comparing objects returned from to different units of work?

Daniel Brückner
A: 

First, let me tell you that what you are doing is HORRIBLE. What does this actually mean

father.Products[553] == product;

Unless you have coded a custom collection, which I doubt you did there is no way that would work.

  • Are you removing product that is already at index 553 from collection? No
  • Are you breaking the relationship between father and product on index 553? No
  • Are you establishing relatinship to a father with a new product? No
  • Are you establishing relationship from new product to father? No

So

  1. Expose products a IEnumerable rather than list
  2. Add Add/Remove methods that will handle relationship syncronozation.

Take a look here how to do it (disregard the actual question)

http://stackoverflow.com/questions/2783622/how-to-map-it-hasone-x-references/2803860#2803860

epitka