O'Reilly logo

Code-First Development with Entity Framework by Sergey Barskiy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Deleting a row from the database

Now let's try to delete a record from the database. First of all, we need to find a row to delete. If you look at the update example, you will see exactly how you can do this. In this example, we will employ a slightly different technique, finding a row by its primary key. In our example, it is the PersonId property's value. Just find a value to delete by running the code from a query example and writing down the appropriate value of the PersonId property. Once we have this value, we can use the Find method of DbSet to locate the correct object. Finally, we will mark the object as deleted using the DbContext API's Remove method, as shown in the following code:

using (var context = new Context()) { var personId = ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required