2011-11-08 37 views
0

當我運行下面的代碼:DeleteOnSubmit:記錄不會被刪除,也不例外拋出

Public Function DeleteSessionItem(ByVal thisSessionItem As SessionItem) As Boolean 
    Using db As New SEMDBDataContext(My.Settings.SEMDBConnectionString) 
     db.DeferredLoadingEnabled = False 
     If thisSessionItem.Modified IsNot Nothing Then 
      db.Log = Console.Out 
      db.SessionItems.Attach(thisSessionItem, True) 
      db.SessionItems.DeleteOnSubmit(thisSessionItem) 
      Try 
       DeleteSessionItem = db.GetChangeSet.Deletes.Count > 0 
       db.SubmitChanges() 
      Catch ex As Exception 
       DeleteSessionItem = False 
      End Try 
     End If 
    End Using 
End Function 

沒有異常拋出,變更集刪除計數爲1和SQL語句是:

DELETE FROM [dbo].[SessionItems] WHERE ([ItemID] = @p0) AND ([Modified] = @p1) 
-- @p0: Input BigInt (Size = 0; Prec = 0; Scale = 0) [61] 
-- @p1: Input Timestamp (Size = 8; Prec = 0; Scale = 0) [SqlBinary(8)] 
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1 

SELECT NULL AS [EMPTY] 
FROM [dbo].[SessionItems] AS [t0] 
WHERE [t0].[ItemID] = @p0 
-- @p0: Input BigInt (Size = 0; Prec = 0; Scale = 0) [61] 
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1 

但該記錄不會從SessionItems表中刪除。這是一個子表(表Sessions),而SessionItems沒有子表。

任何想法爲什麼記錄沒有被刪除?

回答

1

在更新DBML文件時,我必須不小心從以前版本的數據庫中拖出一個表。然後我在項目的設置列表中有兩個連接字符串。我在上面的代碼中使用了舊的連接字符串。使用新的連接字符串允許上面的代碼工作。