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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.