Hi,
I'm writing a Generic for LINQ to SQL CUD. I
'Generic Insert
Public Shared Sub Add(Of T As Class)(ByVal entity As T)
Using db As New APIUDataContext()
db.GetTable(Of T)().InsertOnSubmit(entity)
db.SubmitChanges()
End Using
The genric Insert is working good.
'Generic Update
Public Shared Sub Update(Of T As Class)(ByVal oldEntity As T, ByVal newEntity As T)
Dim db As New DemoDataContext()
db.GetTable(Of T)().Attach(newEntity, oldEntity)
db.SubmitChanges()
End Sub
'Use Generic Update
Dim oldEntity As New TestAuthor
oldEntity.Id = 4
oldEntity.FirstName = "James"
Dim newEntity As New TestAuthor
newEntity.FirstName = TextBox1.Text
newEntity.LastName = TextBox2.Text
GenericCUD.Update(oldEntity, newEntity)
Error message from Generic Update.
Value of member 'Id' of an object of type 'TestAuthor' changed. A member defining the identity of the object cannot be changed. Consider adding a new object with new identity and deleting the existing one instead.
What do I need to modify the Generic Update? Thank you.