我的 Entity Framework 4 模型(与 MS SQL Server Express 一起使用)中有一个多对多关系:Patient-PatientDevice-Device。我正在使用 Poco,所以我的 PatientDevice 类如下所示:
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;
//}
}
当我这样做时一切正常:
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);
但是如果我在 PatientDevice-class 中取消注释 GetHashCode,患者仍然有之前添加的 PatientDevice。
覆盖 GetHashCode 并返回 Id 有什么问题?