2012-09-14 86 views
0

我嘗試使用相同的數據兩次更新,但它拋出異常LinqToSQL無法更新兩次

public void UpdateOnSubmit<T>(T data) where T : class 
    { 
     lock (_lockObj) 
     { 
      using (DataModel dx = new DataModel(this._adapter.ConnectionString)) 
      { 
       dx.GetTable<T>().Attach(data); 
       dx.Refresh(RefreshMode.KeepCurrentValues, data); 
       dx.SubmitChanges(); 
      } 
     } 
    } 

的例外是

An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported. 

首先更新是成功的,但不是在第二位。在此先感謝

問候,

布賴恩

回答

0

好看起來像我又找到了解決辦法。我只需將DeferredLoadingEnabled設置爲false,它就像魅力一樣工作。

public void UpdateOnSubmit<T>(T data) where T : class 
    { 
     lock (_lockObj) 
     { 
      using (DataModel dx = new DataModel(this._adapter.ConnectionString)) 
      { 
       dx.DeferredLoadingEnabled = false; 
       dx.GetTable<T>().Attach(data); 
       dx.Refresh(RefreshMode.KeepCurrentValues, data); 
       dx.SubmitChanges(); 
      } 
     } 
    }