What I would like to do is the following. I have a single table, Products, containing a private key ProductID. Instead of having SQL Server auto-increment ProductID on inserts, I want to increment it in the DataContext partial method "InsertProduct":
Partial Public Class MyDataContext
Private Sub InsertProduct(ByVal instance As Product)
Dim id As Integer = Me.Products.Max(Function(p As Product) p.ProductID) + 1
instance.ProductID = id
Me.ExecuteDynamicInsert(instance)
End Sub
End Class
However, this will only work when inserting the first Product instance. When attempting to insert a second instance, the id retrieved is the same as for the first,
Using context As New MyDataContext
Dim product1 As New Product
context.Products.InsertOnSubmit(product1)
context.SubmitChanges() 'This works
Dim product2 As New Product
context.Products.InsertOnSubmit(product2)
context.SubmitChanges() 'DuplicateKeyException
End Using
Am I missing something obvious here?