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?