当我运行以下代码时:
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
不抛出任何异常,ChangeSet Deletes计数为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没有子表。
知道记录没有被删除的原因吗?
答案 0 :(得分:1)
在更新DBML文件时,我必须意外地从以前版本的数据库中拖出一个表。然后我在项目的设置列表中有两个连接字符串。我在上面的代码中使用旧的连接字符串。使用新的连接字符串可以使上面的代码工作。