是否可以对具有复合主键的实体使用SourceCache?

问题描述 投票:0回答:1

我正在使用Northwind数据库。另外,我正在使用ReactiveUI和动态数据,因此我使用SourceCache来处理记录的收集。

[如果entite的主键包含一个字段,那么我可以轻松地使用SourceCache,例如让我们为Employee实体创建SourceCache:

employeesList = new SourceCache<Employee, int>(a => a.EmployeeID);

但是我不能将SourceCache用于Order_Details实体,因为该实体具有复合主键。

是否可以创建具有复合主键的Entity SourceCache?

c# wpf dynamic-data reactiveui
1个回答
0
投票

DynamicData使用EqualityComparer<TKey>.Default检查两个元素是否彼此相等。您可以在复合主键类上实现自己的IEqualityComparer<TKey>,以提供相等性对复合键的含义。

public class MyCompoundKey : IEqualityComparer<MyCompoundKey>
{
    public bool Equals(MyCompoundKey b1, MyCompoundKey b2)
    {
        if (b2 == null && b1 == null)
           return true;
        else if (b1 == null || b2 == null)
           return false;
        else if(b1.Height == b2.Height && b1.Length == b2.Length
                            && b1.Width == b2.Width)
            return true;
        else
            return false;
    }

    public int GetHashCode(MyCompoundKey bx)
    {
        int hCode = bx.Height ^ bx.Length ^ bx.Width;
        return hCode.GetHashCode();
    }

    public int Height { get; set; }
    public int Length { get; set; }
    public int Width { get; set; }

    public string Name { get; set; }
}

在上面的示例中,它在相等性检查中忽略了名称。

© www.soinside.com 2019 - 2024. All rights reserved.