Updating Entities

Updating entities is a little bit different from adding and deleting in that there is no Update method in either the DbContext or the DbSet classes. You get the instance of the object you want to update, change its properties, and then invoke SaveChanges. The following code demonstrates this:

Sub UpdateProduct()    Try        Dim check = northwindContext.Products.                    Single(Function(p) p.                    ProductName = "Italian spaghetti")        check.Discontinued = True        check.UnitsInStock = 30        northwindContext.SaveChanges()        'Product does not exist    Catch ex As InvalidOperationException    Catch ex As UpdateException    End TryEnd Sub

Just remember to check ...

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.