none
Remove from entity set does not remove from navigational property when GetHashCode overridden RRS feed

  • Question

  • I have an association between entity Edition(the principal) and entity Volume. When I remove a Volume entity from its entity set, it is not removed from the navigational property Edition.Volumes. The primary key of Volume is a composite consisting of the foreign key EditionID plus VolID. Both of these are integers. This happens only when I override Volume's GetHashCode and only newly added Volume entities. Volume entities that are loaded rather than added I can remove with no problem. 

    My Edition.Volumes property is defined as follows:

    Public Overridable Property Volumes As New HashSet(Of Volume)
    

    In fact, all of my navigational collection properties are defined this way , but this is the only one with the problem and the only one with a composite primary key. 

    My Volume's GetHasCode is defined as follows:

    Public Overrides Function GetHashCode() As Integer
         Return (Me.EditionID * 1000) + Me.VolID
    End Function

    All my other GetHasCode functions return the entity's primary key since they all have single integer primary keys. And all my other entities have no problem.

    I'm using Entity Framework 4.1 on a Window 7 64 bit machine. 

    So, is this happening because Volume has a composite key or because it has a foreign key within the primary key? Am I doing something wrong?

    Friday, July 6, 2012 10:17 AM

Answers

  • I found the problem. When I initialized a new Volume, I was setting the navigational property Edition and not the foreign key EditionID and I set it before I set VolID.  I did not realize that setting a Edition would cause this Volume to be added to Edition.Volumes even though Volume is still detached.  And since I did this before setting VolID, my GetHashCode would return the wrong answer.

    I did the same thing with my other entities but GetHashCode would return zero, which I guess the hash sets could handle.

    I guess it's a good practice not to mess with navigational properties until after the entity is added.

    • Marked as answer by DavidSherwood Friday, July 6, 2012 12:56 PM
    Friday, July 6, 2012 12:56 PM

All replies

  • I found the problem. When I initialized a new Volume, I was setting the navigational property Edition and not the foreign key EditionID and I set it before I set VolID.  I did not realize that setting a Edition would cause this Volume to be added to Edition.Volumes even though Volume is still detached.  And since I did this before setting VolID, my GetHashCode would return the wrong answer.

    I did the same thing with my other entities but GetHashCode would return zero, which I guess the hash sets could handle.

    I guess it's a good practice not to mess with navigational properties until after the entity is added.

    • Marked as answer by DavidSherwood Friday, July 6, 2012 12:56 PM
    Friday, July 6, 2012 12:56 PM
  • Hi DavidSherwood,

    Welcome to MSDN Forum.

    I'm glad to hear that you have solved the issue, and thanks for your sharing.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 9, 2012 2:14 AM
    Moderator