为什么我无法在 EF4 中的多对多实体上重写 GetHashCode?

发布于 2024-10-02 12:22:58 字数 1109 浏览 0 评论 0原文

我的 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 类中的 GetHashCode,患者仍然拥有之前添加的 PatientDevice。

重写 GetHashCode 并返回 Id 有什么问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔岸观火 2024-10-09 12:22:59

原因很可能是类类型不是哈希码的一部分,并且实体框架很难区分不同的类型。

请尝试以下操作:

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

另一个问题是,在某些情况下,GetHashCode() 的结果在对象的生命周期内可能不会改变,这些可能适用于实体框架。这与创建时的 Id begin 0 一起也会带来问题。

GetHashCode() 的替代方法是:

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;
}

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文