I deleted a record by using ExecuteCommand between Linq to SQL insertions as below.
At the last call of SubmitChanges, I got below error :
Cannot add an entity with a key that is already in use.
If I use Linq to SQL for deleting, I get no problem, but ExecuteCommand
is a released code, so I can't change it.
How should I insert deleted record again ?
Below is my Code :
Using db = New SomeDataContext(...)
db.Connection.Open()
db.Transaction = db.Connection.BeginTransaction()
' Insert
db.Student.InsertOnSubmit(New Student() With {.Id = 1, .Name = "a"})
db.SubmitChanges()
' Delete(ExecuteCommand)
db.ExecuteCommand("delete from Student where Id = 1")
db.SubmitChanges()
'' Delete(Linq to SQL)
'db.Student.DeleteOnSubmit(db.Student.Single(Function(m) m.Id = 1))
'db.SubmitChanges()
' Insert
db.Student.InsertOnSubmit(New Student() With {.Id = 1, .Name = "a"})
db.SubmitChanges()
End Using
I am using Visual Studio 2013 with SQL Server access.