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.

3.2 Creating new rows in a table

 
 

3.2.1 Creating a single entity on its own

 
 

3.2.2 Creating a book with a review

 
 

3.3 Updating database rows

 
 
 

3.3.1 Handling disconnected updates in a web application

 
 
 

3.4 Handling relationships in updates

 
 
 
 

3.4.1 Principal and dependent relationships

 
 
 

3.4.2 Updating one-to-one relationships—adding a PriceOffer to a book

 

3.4.3 Updating one-to-many relationships—adding a review to a book

 

3.4.4 Updating many-to-many relationships—changing a book’s authors

 
 

3.4.5 Advanced feature—updating relationships via foreign keys

 

3.5 Deleting entities

 
 
 

3.5.1 Using a soft delete—using model-level query filters to “hide” entities

 
 

3.5.2 Deleting a dependent-only entity—no relationships

 
 
 

3.5.3 Deleting a principal entity that has relationships

 
 

Summary

 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest