2016-08-09 37 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覆蓋解決了這個問題。

相關問題