3 Changing the database content
This chapter covers
- Creating a new row in a database table
- Updating existing rows in a database table for two types of applications
- Updating entities with one-to-one, one-to-many, and many-to-many relationships
- Deleting single entities, and entities with relationships, from a database
Chapter 2 covered querying a database. This chapter moves on to changing the content of a database. Changing data has three distinct parts: creating new rows in a database table, updating existing rows in a database table, and deleting rows in a database table, which I cover in that order. Create, update, and delete, along with read (which is query in EF Core terms) are database terms for what’s happening, and the foursome is often shortened to CRUD.
You’ll use the same database as in chapter 2, which has the Book
, PriceOffer
, Review
, BookAuthor
, and Author
entity classes. These provide a good selection of property types and relationships that you can use to learn the various issues and approaches to changing data in a database via EF Core.
3.1 Introducing EF Core’s entity State
Before I start describing the methods to add, update, or delete entities, I want to introduce you to EF Core’s entity property, called State.
This provides another look under the hood at the way EF Core does things. You can skip this section, but it can help you understand what’s going on when you add, update, or delete entities.