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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:57:48+00:00 2026-05-18T02:57:48+00:00

I have a many-to-many relationship in my Entity Framework 4 model (which works with

  • 0

I have a many-to-many relationship in my Entity Framework 4 model (which works with a MS SQL Server Express): Patient-PatientDevice-Device. I’m using Poco, so my PatientDevice-class looks like this:

public class PatientDevice
{
    protected virtual Int32 Id { get; set; }
    protected virtual Int32 PatientId { get; set; }
    public virtual Int32 PhysicalDeviceId { get; set; }
    public virtual Patient Patient { get; set; }
    public virtual Device Device { get; set; }

    //public override int GetHashCode()
    //{
    //    return Id;
    //}
}

All works well when I do this:

var context = new Entities();
var patient = new Patient();
var device = new Device();

context.PatientDevices.AddObject(new PatientDevice { Patient = patient, Device = device });
context.SaveChanges();

Assert.AreEqual(1, patient.PatientDevices.Count);

foreach (var pd in context.PatientDevices.ToList())
{
    context.PatientDevices.DeleteObject(pd);
}
context.SaveChanges();

Assert.AreEqual(0, patient.PatientDevices.Count);

But if I uncomment GetHashCode in PatientDevice-class, the patient still has the PatientDevice added earlier.

What is wrong in overriding GetHashCode and returning the Id?

  • 1 1 Answer
  • 3 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-05-18T02:57:49+00:00Added an answer on May 18, 2026 at 2:57 am

    The reason may very well be that the class type is not part of the hash code, and that the entity framework has difficulty distinguishing between the different types.

    Try the following:

    public override int GetHashCode()
    {
        return Id ^ GetType().GetHashCode();
    }
    

    Another problem is that the result of GetHashCode() may not change during the lifetime of an object under certain circumstances, and these may apply for the entity framework. This together with the Id begin 0 when it’s created also poses problems.

    An alternative of GetHashCode() is:

    private int? _hashCode;
    
    public override int GetHashCode()
    {
        if (!_hashCode.HasValue)
        {
            if (Id == 0)
                _hashCode.Value = base.GetHashCode();
            else
                _hashCode.Value = Id;
                // Or this when the above does not work.
                // _hashCode.Value = Id ^ GetType().GetHashCode();
        }
    
        return _hasCode.Value;
    }
    

    Taken from http://nhforge.org/blogs/nhibernate/archive/2008/09/06/identity-field-equality-and-hash-code.aspx.

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

Sidebar

Related Questions

I have an Entity Framework 4 model, with 2 entities containing many-to-many relationship, so
I am using Entity Framework 4. I have a many-to-many association (relationship) between two
General Info: ASP.NET MVC app using ADO.NET Entity Framework SQL Server 2005 I have
I'm using Entity Framework 4.1 and I have a one to many relationship. When
I have a JPA object which has a many-to-many relationship like this: @Entity public
I have created a very simple Entity Framework 4.0 model using VS2010 Professional. Ignore
I have an Entity Framework model with one-to-many relationships: Client entity could have many
I have a many-to-many relationship between 2 entities in Entity Framework, like here .
I'm using Entity Framework 4.1 and trying to create a one-to-many relationship. In my
I have 2 Entity Framework entity sets with many-to-many relationship (compromised of 3 DB

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.