2016-08-09 58 views
0

我在SQL Server(双重键,无外键)中有一个非常基本的表。我已经使用SQLMetal生成了映射代码。我也扩展了自动生成的部分类,所以我可以实现IEquatable。问题是,一旦我实现了IEquatable,我就失去了使用我的SQLMetal生成的类更新记录的能力。当提交的变化,我得到以下异常:使用SQLMetal生成的类更新表时,关键字'WHERE'附近的语法不正确

不正确的语法关键字附近的“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( [@P1] AND([TheValue] = @ p2)
- @ p0: 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:Input Int(Size = -1; Prec = 0;标尺= 0)[0123]
- @ p1:输入NVarChar(Size = 4000; Prec = 0; Scale = 0)[Red]
- @ p2:输入NVarChar(Size = 4000; Prec = 0;标度= 0)[8f6e72ee-f89e-40f3-830f-18e8b4b40f9e]
- @ P3:输入的NVarChar(尺寸= 4000; PREC = 0;标度= 0)[1ecaff9d-d460-4f3e-b35d-138ddeb2fb63]

这是行为吗?有没有办法避开它?

回答

0

事实证明,使用DataContext跟踪更改的能力覆盖了GetHashCode()方法。删除GetHashCode覆盖解决了这个问题。

相关问题