Deleting Entities

Deleting an entity works similarly to update, at least for retrieving the entity instance. Deletion is performed by invoking the DeleteOnSubmit method, which works opposite to the InsertOnSubmit. The following is an example, which also checks whether the entity exists:

Sub DeleteProduct(productInstance As Product)    If productInstance Is Nothing Then        Throw New NullReferenceException    Else        northwind.Products.DeleteOnSubmit(productInstance)        SaveChanges()    End IfEnd Sub

Remember how the custom SaveChanges method invokes the NorthwindDataContext.SubmitChanges one. The following code shows invoking the previous method for performing a product deletion:

Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.