nhibernate Iesi ISet fails to Remove()

本秂侑毒 提交于 2019-12-03 17:14:16

are you sure this is the actual code executing? It seems to me that even if you override Equals and GetHashCode in some malfunctioning way, as soon as you find an Asset and target is assigned with an object from _assets, the Remove method should never fail when called with target as it definitly is contained in the Set. I did a short test and the set behaved as expected.

I think I just faced the same issue, and the "problem" was that I added the item to the HashedSet and afterwards changed it (to assign IDs), changing the result of the GetHashCode.

I think this means that the object ended up in a different bucket in the inner dictionary, hence Contains returning false.

Yep I have an almost identical implementation of your base Entity object (we must have read the same book :D).

As Bruno suggests the problem lies with the fact that GetHashCode returns different values (either the base objects object reference, or a primary key value). in most situations this is not a problem for me since I rarely change an object once it has been saved but under some circumstances, as per below, this can cause problems:

FuelTank newtank = new FuelTank();

// Below stores tank in the Tanks Set using the 
//objects reference identity (since it is a new object)
vessel.Tanks.Add(newtank); 

//returns true because newtank.Id has not changed (remove 
//uses Contains internally  to see if an object can be removed)
vessel.Tanks.Contains(newtank); 

//now newtank.Id changes because a primary key is 
//generated for it at this point
Repository.Save(vessel); 

// returns false because newtank is still stored under 
//its object reference in the set but its id is now the id of a PK
vessel.Tanks.Contains(newtank);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!