Deleting Entities

Deleting entities is also a simple task. You first need to get the instance of the entity you want to remove and then invoke the DbSet(Of T).Remove method. The following code shows how to get the instance of the specified product and then to remove it first from the model and then from the database:

Sub DeleteProduct()    Try        Dim check = northwindContext.Products.                    Single(Function(p) p.                    ProductName = "Italian spaghetti")        northwindContext.Products.Remove(check)        northwindContext.SaveChanges()        'Does not exist    Catch ex As InvalidOperationException    End TryEnd Sub

Same as in previous code, we take advantage of the Single method that ...

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.