开发者

load an object twice and change one of them, the other one doesnt changed

Fat开发者_JAVA百科her 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.


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?


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)

How to map it? HasOne x References

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜