6 Tips and techniques for reading and writing with EF Core

 

This chapter covers

  • Selecting the right approach to read data from the database
  • Writing queries that perform well on the database side
  • Avoiding problems when you use Query Filters and special LINQ commands
  • Using AutoMapper to write Select queries more quickly
  • Writing code to quickly copy and delete entities in the database

The first four chapters covers different ways to read/write to a database and in chapter 5 you used that information to build the Book App - an ASP.NET Core web application. This chapter brings together lots of different tips and techniques for reading and writing data with EF Core.

The chapter is split into two sections: reading from the database and writing to the database. Each section covers certain read/write issues you may come across, but at the same time explains how EF Core achieves this solution. The aim is to give you lots of practical tips by solving different problems and at the same time deepen your knowledge of how EF Core works. The tips are useful but, in the long run, becoming an expert on EF Core is going to make you a better developer.

TIP

Don’t forget that the companion Git repo https://github.com/JonPSmith/EfCoreinAction-SecondEdition contains unit tests for every chapter of the book. For this chapter, look in the Test project in the master branch for classes starting with Ch06_. Sometimes seeing the code is quicker than reading the words.

6.1     Reading from the database

6.1.1     Exploring the relational fixup stage in a query

6.1.2     Understanding what AsNoTracking and its variant do

6.1.3     Reading in hierarchical data efficiently

6.1.4     Understanding how the Include method works

6.1.5     Applying AsNoTracking after a query has finished

6.1.6     Making loading navigational collections fail-safe

6.1.7     Using Global Query Filters in real-world situations

6.1.8     Considering LINQ commands that need special attention

6.1.9     Using AutoMapper to automate building Select queries

6.1.10     Evaluating how EF Core creates an entity class when reading data in

6.2     Writing to the database with EF Core

6.2.1     Evaluating how EF Core writes entities/relationships to the database

6.2.2     Evaluating how DbContext handles writing out entities/relationships

6.2.3     A quick way to copy data with relationships

6.2.4     A quick way to delete an entity

6.3     Summary