0

我在 SQL Server 中有一个非常基本的表(双键,没有外键)。我已经使用 SQLMetal 生成了映射代码。我还扩展了自动生成的部分类,所以我可以实现 IEquatable。问题是,一旦我实现了 IEquatable,我就无法使用我的 SQLMetal 生成的类来更新记录。提交更改时,我收到以下异常:

关键字“WHERE”附近的语法不正确

下面的示例代码说明了这个问题。在实现 IEquatable 之前它运行良好:

var connection = "Your connection string";
var dbInsert = new DBTest(connection);
var recordToInsert = new TestTable()
{
    PrimaryKey1 = 123,
    PrimaryKey2 = "Red",
    TheValue = Guid.NewGuid().ToString(),
};
dbInsert.TestTable.InsertOnSubmit(recordToInsert);
dbInsert.SubmitChanges();

var dbEdit = new DBTest(connection);
dbEdit.Log = Console.Out;
var ti1 = dbEdit.TestTable.Single(x => x.PrimaryKey1 == 123 && x.PrimaryKey2 == "Red");
ti1.TheValue = Guid.NewGuid().ToString();
dbEdit.SubmitChanges();

这是我对 IEquatable 的实现(由 ReSharper 自动生成):

public partial class TestTable : IEquatable<TestTable>
{
    public bool Equals(TestTable other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return _PrimaryKey1 == other._PrimaryKey1 && string.Equals(_PrimaryKey2, other._PrimaryKey2) && string.Equals(_TheValue, other._TheValue);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((TestTable)obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            var hashCode = _PrimaryKey1;
            hashCode = (hashCode * 397) ^ (_PrimaryKey2 != null ? _PrimaryKey2.GetHashCode() : 0);
            hashCode = (hashCode * 397) ^ (_TheValue != null ? _TheValue.GetHashCode() : 0);
            return hashCode;
        }
    }
}

查看在输出窗口中打印的查询。实现 IEquatable 时,SET 子句为空(并导致抛出异常):

UPDATE [dbo].[TestTable]
SET
WHERE ([PrimaryKey1] = @p0) AND ([PrimaryKey2] = @p1) AND ([TheValue] = @p2)
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [123]
-- @p1: 输入 NVarChar (Size = 4000; Prec = 0; Scale = 0) [Red]
-- @p2: 输入 NVarChar (Size = 4000; Prec = 0; Scale = 0) [8dedfdca-84e9-4b7a-9268-4bbdde2e9ad2]

这是没有实现 IEquatable 的相同输出:

UPDATE [dbo].[TestTable]
SET [TheValue] = @p3
WHERE ([PrimaryKey1] = @p0) AND ([PrimaryKey2] = @p1) AND ([TheValue] = @p2)
-- @p0: 输入 Int ( Size = -1; Prec = 0; Scale = 0) [123]
-- @p1: 输入 NVarChar (Size = 4000; Prec = 0; Scale = 0) [Red]
-- @p2: 输入 NVarChar (Size = 4000 ; Prec = 0; Scale = 0) [8f6e72ee-f89e-40f3-830f-18e8b4b40f9e]
-- @p3: Input NVarChar (Size = 4000; Prec = 0; Scale = 0) [1ecaff9d-d460-4f3e-b35d-138ddeb2fb63 ]

这种行为是预期的吗?有没有办法绕过它?

4

1 回答 1

1

事实证明,重写 GetHashCode() 方法会破坏 DataContext 跟踪更改的能力。删除 GetHashCode 覆盖解决了该问题。

于 2016-09-01T19:34:46.723 回答