Entity Framework Core in Action, Second Edition

Book description

Entity Framework Core in Action, Second Edition teaches you to write flawless database interactions for .NET applications. Following relevant examples from author Jon Smith’s extensive experience, you’ll progress quickly from EF basics to advanced techniques. In addition to the latest EF features, this book addresses performance, security, refactoring, and unit testing. This updated edition also contains new material on NoSQL databases.

Table of contents

  1. Entity Framework Core in Action
  2. Copyright
  3. Praise for the first edition
  4. contents
  5. front matter
    1. foreword
    2. preface
    3. acknowledgments
    4. about this book
      1. Who should read this book?
      2. How this book is organized
      3. About the code
      4. Code conventions
      5. liveBook discussion forum
      6. Online resources
    5. about the author
    6. about the cover illustration
  6. Part 1 Getting started
  7. 1 Introduction to Entity Framework Core
    1. 1.1 What you’ll learn from this book
    2. 1.2 My “lightbulb moment” with Entity Framework
    3. 1.3 Some words for existing EF6.x developers
    4. 1.4 An overview of EF Core
      1. 1.4.1 The downsides of O/RMs
    5. 1.5 What about NoSQL?
    6. 1.6 Your first EF Core application
      1. 1.6.1 What you need to install
      2. 1.6.2 Creating your own .NET Core console app with EF Core
    7. 1.7 The database that MyFirstEfCoreApp will access
    8. 1.8 Setting up the MyFirstEfCoreApp application
      1. 1.8.1 The classes that map to the database: Book and Author
      2. 1.8.2 The application’s DbContext
    9. 1.9 Looking under the hood of EF Core
      1. 1.9.1 Modeling the database
      2. 1.9.2 Reading data from the database
      3. 1.9.3 Updating the database
    10. 1.10 The stages of development of EF Core
    11. 1.11 Should you use EF Core in your next project?
      1. 1.11.1 .NET is the future software platform, and it’s fast!
      2. 1.11.2 Open source and open communication
      3. 1.11.3 Multiplatform applications and development
      4. 1.11.4 Rapid development and good features
      5. 1.11.5 Well supported
      6. 1.11.6 Always high-performance
    12. 1.12 When should you not use EF Core?
    13. Summary
  8. 2 Querying the database
    1. 2.1 Setting the scene: Our book-selling site
      1. 2.1.1 The Book App’s relational database
      2. 2.1.2 Other relationship types not covered in this chapter
      3. 2.1.3 The database showing all the tables
      4. 2.1.4 The classes that EF Core maps to the database
    2. 2.2 Creating the application’s DbContext
      1. 2.2.1 Defining the application’s DbContext: EfCoreContext
      2. 2.2.2 Creating an instance of the application’s DbContext
      3. 2.2.3 Creating a database for your own application
    3. 2.3 Understanding database queries
      1. 2.3.1 Application’s DbContext property access
      2. 2.3.2 A series of LINQ/EF Core commands
      3. 2.3.3 The execute command
      4. 2.3.4 The two types of database queries
    4. 2.4 Loading related data
      1. 2.4.1 Eager loading: Loading relationships with the primary entity class
      2. 2.4.2 Explicit loading: Loading relationships after the primary entity class
      3. 2.4.3 Select loading: Loading specific parts of primary entity class and any relationships
      4. 2.4.4 Lazy loading: Loading relationships as required
    5. 2.5 Using client vs. server evaluation: Adapting data at the last stage of a query
    6. 2.6 Building complex queries
    7. 2.7 Introducing the architecture of the Book App
    8. 2.8 Adding sorting, filtering, and paging
      1. 2.8.1 Sorting books by price, publication date, and customer ratings
      2. 2.8.2 Filtering books by publication year, categories, and customer ratings
      3. 2.8.3 Other filtering options: Searching text for a specific string
      4. 2.8.4 Paging the books in the list
    9. 2.9 Putting it all together: Combining Query Objects
      1. Summary
  9. 3 Changing the database content
    1. 3.1 Introducing EF Core’s entity State
    2. 3.2 Creating new rows in a table
      1. 3.2.1 Creating a single entity on its own
      2. 3.2.2 Creating a book with a review
    3. 3.3 Updating database rows
      1. 3.3.1 Handling disconnected updates in a web application
    4. 3.4 Handling relationships in updates
      1. 3.4.1 Principal and dependent relationships
      2. 3.4.2 Updating one-to-one relationships: Adding a PriceOffer to a book
      3. 3.4.3 Updating one-to-many relationships: Adding a review to a book
      4. 3.4.4 Updating a many-to-many relationship
      5. 3.4.5 Advanced feature: Updating relationships via foreign keys
    5. 3.5 Deleting entities
      1. 3.5.1 Soft-delete approach: Using a global query filter to hide entities
      2. 3.5.2 Deleting a dependent-only entity with no relationships
      3. 3.5.3 Deleting a principal entity that has relationships
      4. 3.5.4 Deleting a book with its dependent relationships
    6. Summary
  10. 4 Using EF Core in business logic
    1. 4.1 The questions to ask and the decisions you need to make before you start coding
      1. 4.1.1 The three levels of complexity of your business logic code
    2. 4.2 Complex business logic example: Processing an order for books
    3. 4.3 Using a design pattern to implement complex business logic
      1. 4.3.1 Five guidelines for building business logic that uses EF Core
    4. 4.4 Implementing the business logic for processing an order
      1. 4.4.1 Guideline 1: Business logic has first call on defining the database structure
      2. 4.4.2 Guideline 2: Business logic should have no distractions
      3. 4.4.3 Guideline 3: Business logic should think that it’s working on in-memory data
      4. 4.4.4 Guideline 4: Isolate the database access code into a separate project
      5. 4.4.5 Guideline 5: Business logic shouldn’t call EF Core’s SaveChanges
      6. 4.4.6 Putting it all together: Calling the order-processing business logic
      7. 4.4.7 Placing an order in the Book App
      8. 4.4.8 The pros and cons of the complex business logic pattern
    5. 4.5 Simple business logic example: ChangePriceOfferService
      1. 4.5.1 My design approach for simple business logic
      2. 4.5.2 Writing the ChangePriceOfferService code
      3. 4.5.3 The pros and cons of this business logic pattern
    6. 4.6 Validation business logic example: Adding review to a book, with checks
      1. 4.6.1 The pros and cons of this business logic pattern
    7. 4.7 Adding extra features to your business logic handling
      1. 4.7.1 Validating the data that you write to the database
      2. 4.7.2 Using transactions to daisy-chain a sequence of business logic code
      3. 4.7.3 Using the RunnerTransact2WriteDb class
    8. Summary
  11. 5 Using EF Core in ASP.NET Core web applications
    1. 5.1 Introducing ASP.NET Core
    2. 5.2 Understanding the architecture of the Book App
    3. 5.3 Understanding dependency injection
      1. 5.3.1 Why you need to learn about DI in ASP.NET Core
      2. 5.3.2 A basic example of dependency injection in ASP.NET Core
      3. 5.3.3 The lifetime of a service created by DI
      4. 5.3.4 Special considerations for Blazor Server applications
    4. 5.4 Making the application’s DbContext available via DI
      1. 5.4.1 Providing information on the database’s location
      2. 5.4.2 Registering your application’s DbContext with the DI provider
      3. 5.4.3 Registering a DbContext Factory with the DI provider
    5. 5.5 Calling your database access code from ASP.NET Core
      1. 5.5.1 A summary of how ASP.NET Core MVC works and the terms it uses
      2. 5.5.2 Where does the EF Core code live in the Book App?
    6. 5.6 Implementing the book list query page
      1. 5.6.1 Injecting an instance of the application’s DbContext via DI
      2. 5.6.2 Using the DbContext Factory to create an instance of a DbContext
    7. 5.7 Implementing your database methods as a DI service
      1. 5.7.1 Registering your class as a DI service
      2. 5.7.2 Injecting ChangePubDateService into the ASP.NET action method
      3. 5.7.3 Improving registering your database access classes as services
    8. 5.8 Deploying an ASP.NET Core application with a database
      1. 5.8.1 Knowing where the database is on the web server
      2. 5.8.2 Creating and migrating the database
    9. 5.9 Using EF Core’s migration feature to change the database’s structure
      1. 5.9.1 Updating your production database
      2. 5.9.2 Having your application migrate your database on startup
    10. 5.10 Using async/await for better scalability
      1. 5.10.1 Why async/await is useful in a web application using EF Core
      2. 5.10.2 Where should you use async/await with database accesses?
      3. 5.10.3 Changing over to async/await versions of EF Core commands
    11. 5.11 Running parallel tasks: How to provide the DbContext
      1. 5.11.1 Obtaining an instance of your application’s DbContext to run in parallel
      2. 5.11.2 Running a background service in ASP.NET Core
      3. 5.11.3 Other ways of obtaining a new instance of the application’s DbContext
    12. Summary
  12. 6 Tips and techniques for reading and writing with EF Core
    1. 6.1 Reading from the database
      1. 6.1.1 Exploring the relational fixup stage in a query
      2. 6.1.2 Understanding what AsNoTracking and its variant do
      3. 6.1.3 Reading in hierarchical data efficiently
      4. 6.1.4 Understanding how the Include method works
      5. 6.1.5 Making loading navigational collections fail-safe
      6. 6.1.6 Using Global Query Filters in real-world situations
      7. 6.1.7 Considering LINQ commands that need special attention
      8. 6.1.8 Using AutoMapper to automate building Select queries
      9. 6.1.9 Evaluating how EF Core creates an entity class when reading data in
    2. 6.2 Writing to the database with EF Core
      1. 6.2.1 Evaluating how EF Core writes entities/relationships to the database
      2. 6.2.2 Evaluating how DbContext handles writing out entities/relationships
      3. 6.2.3 A quick way to copy data with relationships
      4. 6.2.4 A quick way to delete an entity
    3. Summary
  13. Part 2 Entity Framework in depth
  14. 7 Configuring nonrelational properties
    1. 7.1 Three ways of configuring EF Core
    2. 7.2 A worked example of configuring EF Core
    3. 7.3 Configuring by convention
      1. 7.3.1 Conventions for entity classes
      2. 7.3.2 Conventions for parameters in an entity class
      3. 7.3.3 Conventions for name, type, and size
      4. 7.3.4 By convention, the nullability of a property is based on .NET type
      5. 7.3.5 An EF Core naming convention identifies primary keys
    4. 7.4 Configuring via Data Annotations
      1. 7.4.1 Using annotations from System.ComponentModel.DataAnnotations
      2. 7.4.2 Using annotations from System.ComponentModel.DataAnnotations.Schema
    5. 7.5 Configuring via the Fluent API
    6. 7.6 Excluding properties and classes from the database
      1. 7.6.1 Excluding a class or property via Data Annotations
      2. 7.6.2 Excluding a class or property via the Fluent API
    7. 7.7 Setting database column type, size, and nullability
    8. 7.8 Value conversions: Changing data to/from the database
    9. 7.9 The different ways of configuring the primary key
      1. 7.9.1 Configuring a primary key via Data Annotations
      2. 7.9.2 Configuring a primary key via the Fluent API
      3. 7.9.3 Configuring an entity as read-only
    10. 7.10 Adding indexes to database columns
    11. 7.11 Configuring the naming on the database side
      1. 7.11.1 Configuring table names
      2. 7.11.2 Configuring the schema name and schema groupings
      3. 7.11.3 Configuring the database column names in a table
    12. 7.12 Configuring Global Query Filters
    13. 7.13 Applying Fluent API commands based on the database provider type
    14. 7.14 Shadow properties: Hiding column data inside EF Core
      1. 7.14.1 Configuring shadow properties
      2. 7.14.2 Accessing shadow properties
    15. 7.15 Backing fields: Controlling access to data in an entity class
      1. 7.15.1 Creating a simple backing field accessed by a read/write property
      2. 7.15.2 Creating a read-only column
      3. 7.15.3 Concealing a person’s date of birth: Hiding data inside a class
      4. 7.15.4 Configuring backing fields
    16. 7.16 Recommendations for using EF Core’s configuration
      1. 7.16.1 Use By Convention configuration first
      2. 7.16.2 Use validation Data Annotations wherever possible
      3. 7.16.3 Use the Fluent API for anything else
      4. 7.16.4 Automate adding Fluent API commands by class/property signatures
    17. Summary
  15. 8 Configuring relationships
    1. 8.1 Defining some relationship terms
    2. 8.2 What navigational properties do you need?
    3. 8.3 Configuring relationships
    4. 8.4 Configuring relationships By Convention
      1. 8.4.1 What makes a class an entity class?
      2. 8.4.2 An example of an entity class with navigational properties
      3. 8.4.3 How EF Core finds foreign keys By Convention
      4. 8.4.4 Nullability of foreign keys: Required or optional dependent relationships
      5. 8.4.5 Foreign keys: What happens if you leave them out?
      6. 8.4.6 When does By Convention configuration not work?
    5. 8.5 Configuring relationships by using Data Annotations
      1. 8.5.1 The ForeignKey Data Annotation
      2. 8.5.2 The InverseProperty Data Annotation
    6. 8.6 Fluent API relationship configuration commands
      1. 8.6.1 Creating a one-to-one relationship
      2. 8.6.2 Creating a one-to-many relationship
      3. 8.6.3 Creating a many-to-many relationship
    7. 8.7 Controlling updates to collection navigational properties
    8. 8.8 Additional methods available in Fluent API relationships
      1. 8.8.1 OnDelete: Changing the delete action of a dependent entity
      2. 8.8.2 IsRequired: Defining the nullability of the foreign key
      3. 8.8.3 HasPrincipalKey: Using an alternate unique key
      4. 8.8.4 Less-used options in Fluent API relationships
    9. 8.9 Alternative ways of mapping entities to database tables
      1. 8.9.1 Owned types: Adding a normal class into an entity class
      2. 8.9.2 Table per hierarchy (TPH): Placing inherited classes into one table
      3. 8.9.3 Table per Type (TPT): Each class has its own table
      4. 8.9.4 Table splitting: Mapping multiple entity classes to the same table
      5. 8.9.5 Property bag: Using a dictionary as an entity class
      6. Summary
  16. 9 Handling database migrations
    1. 9.1 How this chapter is organized
    2. 9.2 Understanding the complexities of changing your application’s database
      1. 9.2.1 A view of what databases need updating
      2. 9.2.2 Handling a migration that can lose data
    3. 9.3 Part 1: Introducing the three approaches to creating a migration
    4. 9.4 Creating a migration by using EF Core’s add migration command
      1. 9.4.1 Requirements before running any EF Core migration command
      2. 9.4.2 Running the add migration command
      3. 9.4.3 Seeding your database via an EF Core migration
      4. 9.4.4 Handling EF Core migrations with multiple developers
      5. 9.4.5 Using a custom migration table to allow multiple DbContexts to one database
    5. 9.5 Editing an EF Core migration to handle complex situations
      1. 9.5.1 Adding and removing MigrationBuilder methods inside the migration class
      2. 9.5.2 Adding SQL commands to a migration
      3. 9.5.3 Adding your own custom migration commands
      4. 9.5.4 Altering a migration to work for multiple database types
    6. 9.6 Using SQL scripts to build migrations
      1. 9.6.1 Using SQL database comparison tools to produce migration
      2. 9.6.2 Handcoding SQL change scripts to migrate the database
      3. 9.6.3 Checking that your SQL change scripts matches EF Core’s database model
    7. 9.7 Using EF Core’s reverse-engineering tool
      1. 9.7.1 Running EF Core’s reverse-engineering command
      2. 9.7.2 Installing and running EF Core Power Tools reverse-engineering command
      3. 9.7.3 Updating your entity classes and DbContext when the database changes
    8. 9.8 Part 2: Applying your migrations to a database
      1. 9.8.1 Calling EF Core’s Database.Migrate method from your main application
      2. 9.8.2 Executing EF Core’s Database.Migrate method from a standalone application
      3. 9.8.3 Applying an EF Core’s migration via an SQL change script
      4. 9.8.4 Applying SQL change scripts by using a migration tool
    9. 9.9 Migrating a database while the application is running
      1. 9.9.1 Handling a migration that doesn’t contain an application-breaking change
      2. 9.9.2 Handling application-breaking changes when you can’t stop the app
    10. Summary
  17. 10 Configuring advanced features and handling concurrency conflicts
    1. 10.1 DbFunction: Using user-defined functions (UDFs) with EF Core
      1. 10.1.1 Configuring a scalar-valued UDF
      2. 10.1.2 Configuring a table-valued UDF
      3. 10.1.3 Adding your UDF code to the database
      4. 10.1.4 Using a registered UDF in your database queries
    2. 10.2 Computed column: A dynamically calculated column value
    3. 10.3 Setting a default value for a database column
      1. 10.3.1 Using the HasDefaultValue method to add a constant value for a column
      2. 10.3.2 Using the HasDefaultValueSql method to add an SQL command for a column
      3. 10.3.3 Using the HasValueGenerator method to assign a value generator to a property
    4. 10.4 Sequences: Providing numbers in a strict order
    5. 10.5 Marking database-generated properties
      1. 10.5.1 Marking a column that’s generated on an addition or update
      2. 10.5.2 Marking a column’s value as set on insert of a new row
      3. 10.5.3 Marking a column/property as “normal”
    6. 10.6 Handling simultaneous updates: Concurrency conflicts
      1. 10.6.1 Why do concurrency conflicts matter?
      2. 10.6.2 EF Core’s concurrency conflict-handling features
      3. 10.6.3 Handling a DbUpdateConcurrencyException
      4. 10.6.4 The disconnected concurrent update issue
    7. Summary
  18. 11 Going deeper into the DbContext
    1. 11.1 Overview of the DbContext class’s properties
    2. 11.2 Understanding how EF Core tracks changes
    3. 11.3 Looking at commands that change an entity’s State
      1. 11.3.1 The Add command: Inserting a new row into the database
      2. 11.3.2 The Remove method: Deleting a row from the database
      3. 11.3.3 Modifying an entity class by changing the data in that entity class
      4. 11.3.4 Modifying an entity class by calling the Update method
      5. 11.3.5 The Attach method: Start tracking an existing untracked entity class
      6. 11.3.6 Setting the State of an entity directly
      7. 11.3.7 TrackGraph: Handling disconnected updates with relationships
    4. 11.4 SaveChanges and its use of ChangeTracker.DetectChanges
      1. 11.4.1 How SaveChanges finds all the State changes
      2. 11.4.2 What to do if ChangeTracker.DetectChanges is taking too long
      3. 11.4.3 Using the entities’ State within the SaveChanges method
      4. 11.4.4 Catching entity class’s State changes via events
      5. 11.4.5 Triggering events when SaveChanges/SaveChangesAsync is called
      6. 11.4.6 EF Core interceptors
    5. 11.5 Using SQL commands in an EF Core application
      1. 11.5.1 FromSqlRaw/FromSqlInterpolated: Using SQL in an EF Core query
      2. 11.5.2 ExecuteSqlRaw/ExecuteSqlInterpolated: Executing a nonquery command
      3. 11.5.3 AsSqlQuery Fluent API method: Mapping entity classes to queries
      4. 11.5.4 Reload: Used after ExecuteSql commands
      5. 11.5.5 GetDbConnection: Running your own SQL commands
    6. 11.6 Accessing information about the entity classes and database tables
      1. 11.6.1 Using context.Entry(entity).Metadata to reset primary keys
      2. 11.6.2 Using context.Model to get database information
    7. 11.7 Dynamically changing the DbContext’s connection string
    8. 11.8 Handling database connection problems
      1. 11.8.1 Handling database transactions with EF Core’s execution strategy
      2. 11.8.2 Altering or writing your own execution strategy
    9. Summary
  19. Part 3 Using Entity Framework Core in real-world applications
  20. 12 Using entity events to solve business problems
    1. 12.1 Using events to solve business problems
      1. 12.1.1 Example of using domain events
      2. 12.1.2 Example of integration events
    2. 12.2 Defining where domain events and integration events are useful
    3. 12.3 Where might you use events with EF Core?
      1. 12.3.1 Pro: Follows the SoC design principle
      2. 12.3.2 Pro: Makes database updates robust
      3. 12.3.3 Con: Makes your application more complex
      4. 12.3.4 Con: Makes following the flow of the code more difficult
    4. 12.4 Implementing a domain event system with EF Core
      1. 12.4.1 Create some domain events classes to be triggered
      2. 12.4.2 Add code to the entity classes to hold the domain events
      3. 12.4.3 Alter the entity class to detect a change to trigger an event on
      4. 12.4.4 Create event handlers that are matched to the domain events
      5. 12.4.5 Build an Event Runner that finds and runs the correct event handler
      6. 12.4.6 Override SaveChanges and insert the Event Runner before SaveChanges is called
      7. 12.4.7 Register the Event Runner and all the event handlers
    5. 12.5 Implementing an integration event system with EF Core
      1. 12.5.1 Building a service that communicates with the warehouse
      2. 12.5.2 Overriding SaveChanges to handle the integration event
    6. 12.6 Improving the domain event and integration event implementations
      1. 12.6.1 Generalizing events: Running before, during, and after the call to SaveChanges
      2. 12.6.2 Adding support for async event handlers
      3. 12.6.3 Handling multiple event handers for the same event
      4. 12.6.4 Handling event sagas in which one event kicks off another event
    7. Summary
  21. 13 Domain-Driven Design and other architectural approaches
    1. 13.1 A good software architecture makes it easier to build and maintain your application
    2. 13.2 The Book App’s evolving architecture
      1. 13.2.1 Building a modular monolith to enforce the SoC principles
      2. 13.2.2 Using DDD principles both architecturally and on the entity classes
      3. 13.2.3 Applying a clean architecture as described by Robert C. Martin
    3. 13.3 Introduction to DDD at the entity class level
    4. 13.4 Altering the Book App entities to follow the DDD approach
      1. 13.4.1 Changing the properties in the Book entity to read-only
      2. 13.4.2 Updating the Book entity properties via methods in the entity class
      3. 13.4.3 Controlling how the Book entity is created
      4. 13.4.4 Understanding the differences between an entity and a value object
      5. 13.4.5 Minimizing the relationships between entity classes
      6. 13.4.6 Grouping entity classes
      7. 13.4.7 Deciding when the business logic shouldn’t be run inside an entity
      8. 13.4.8 Applying DDD’s bounded context to your application’s DbContext
    5. 13.5 Using your DDD-styled entity classes in your application
      1. 13.5.1 Calling the AddPromotion access method via a repository pattern
      2. 13.5.2 Calling the AddPromotion access method via a class-to-method-call library
      3. 13.5.3 Adding a Review to the Book entity class via a repository pattern
      4. 13.5.4 Adding a Review to the Book entity class via a class-to-method-call library
    6. 13.6 The downside of DDD entities: Too many access methods
    7. 13.7 Getting around performance issues in DDD-styled entities
      1. 13.7.1 Allow database code into your entity classes
      2. 13.7.2 Make the Review constructor public and write nonentity code to add a Review
      3. 13.7.3 Use domain events to ask an event handler to add a review to the database
    8. 13.8 Three architectural approaches: Did they work?
      1. 13.8.1 A modular monolith approach that enforces SoC by using projects
      2. 13.8.2 DDD principles, both architecturally and on the entity classes
      3. 13.8.3 Clean architecture as described by Robert C. Martin
    9. Summary
  22. 14 EF Core performance tuning
    1. 14.1 Part 1: Deciding which performance issues to fix
      1. 14.1.1 “Don’t performance-tune too early” doesn’t mean you stop thinking
      2. 14.1.2 How do you decide what’s slow and needs performance tuning?
      3. 14.1.3 The cost of finding and fixing performance issues
    2. 14.2 Part 2: Techniques for diagnosing a performance issue
      1. 14.2.1 Stage 1: Get a good overview, measuring the user’s experience
      2. 14.2.2 Stage 2: Find all the database code involved in the feature you’re tuning
      3. 14.2.3 Stage 3: Inspect the SQL code to find poor performance
    3. 14.3 Part 3: Techniques for fixing performance issues
    4. 14.4 Using good patterns makes your application perform well
      1. 14.4.1 Using Select loading to load only the columns you need
      2. 14.4.2 Using paging and/or filtering of searches to reduce the rows you load
      3. 14.4.3 Warning: Lazy loading will affect database performance
      4. 14.4.4 Always adding the AsNoTracking method to read-only queries
      5. 14.4.5 Using the async version of EF Core commands to improve scalability
      6. 14.4.6 Ensuring that your database access code is isolated/decoupled
    5. 14.5 Performance antipatterns: Database queries
      1. 14.5.1 Antipattern: Not minimizing the number of calls to the database
      2. 14.5.2 Antipattern: Missing indexes from a property that you want to search on
      3. 14.5.3 Antipattern: Not using the fastest way to load a single entity
      4. 14.5.4 Antipattern: Allowing too much of a data query to be moved into the software side
      5. 14.5.5 Antipattern: Not moving calculations into the database
      6. 14.5.6 Antipattern: Not replacing suboptimal SQL in a LINQ query
      7. 14.5.7 Antipattern: Not precompiling frequently used queries
    6. 14.6 Performance antipatterns: Writes
      1. 14.6.1 Antipattern: Calling SaveChanges multiple times
      2. 14.6.2 Antipattern: Making DetectChanges work too hard
      3. 14.6.3 Antipattern: Not using HashSet<T> for navigational collection properties
      4. 14.6.4 Antipattern: Using the Update method when you want to change only part of the entity
      5. 14.6.5 Antipattern: Startup issue—Using one large DbContext
    7. 14.7 Performance patterns: Scalability of database accesses
      1. 14.7.1 Using pooling to reduce the cost of a new application’s DbContext
      2. 14.7.2 Adding scalability with little effect on overall speed
      3. 14.7.3 Helping your database scalability by making your queries simple
      4. 14.7.4 Scaling up the database server
      5. 14.7.5 Picking the right architecture for applications that need high scalability
    8. Summary
  23. 15 Master class on performance-tuning database queries
    1. 15.1 The test setup and a summary of the four performance approaches
    2. 15.2 Good LINQ approach: Using an EF Core Select query
    3. 15.3 LINQ+UDFs approach: Adding some SQL to your LINQ code
    4. 15.4 SQL+Dapper: Creating your own SQL
    5. 15.5 LINQ+caching approach: Precalculating costly query parts
      1. 15.5.1 Adding a way to detect changes that affect the cached values
      2. 15.5.2 Adding code to update the cached values
      3. 15.5.3 Adding cache properties to the Book entity with concurrency handling
      4. 15.5.4 Adding a checking/healing system to your event system
    6. 15.6 Comparing the four performance approaches with development effort
    7. 15.7 Improving database scalability
    8. Summary
  24. 16 Cosmos DB, CQRS, and other database types
    1. 16.1 The differences between relational and NoSQL databases
    2. 16.2 Introduction to Cosmos DB and its EF Core provider
    3. 16.3 Building a Command and Query Responsibility Segregation (CQRS) system using Cosmos DB
    4. 16.4 The design of a two-database CQRS architecture application
      1. 16.4.1 Creating an event to trigger when the SQL Book entity changes
      2. 16.4.2 Adding events to the Book entity send integration events
      3. 16.4.3 Using the EfCore.GenericEventRunner to override your BookDbContext
      4. 16.4.4 Creating the Cosmos entity classes and DbContext
      5. 16.4.5 Creating the Cosmos event handlers
    5. 16.5 Understanding the structure and data of a Cosmos DB account
      1. 16.5.1 The Cosmos DB structure as seen from EF Core
      2. 16.5.2 How the CosmosClass is stored in Cosmos DB
    6. 16.6 Displaying books via Cosmos DB
      1. 16.6.1 Cosmos DB differences from relational databases
      2. 16.6.2 Cosmos DB/EF Core difference: Migrating a Cosmos database
      3. 16.6.3 EF Core 5 Cosmos DB database provider limitations
    7. 16.7 Was using Cosmos DB worth the effort? Yes!
      1. 16.7.1 Evaluating the performance of the two-database CQRS in the Book App
      2. 16.7.2 Fixing the features that EF Core 5 Cosmos DB database provider couldn’t handle
      3. 16.7.3 How difficult would it be to use this two-database CQRS design in your application?
    8. 16.8 Differences in other database types
    9. Summary
  25. 17 Unit testing EF Core applications
    1. 17.1 An introduction to the unit test setup
      1. 17.1.1 The test environment: xUnit unit test library
      2. 17.1.2 A library I created to help with unit testing EF Core applications
    2. 17.2 Getting your application’s DbContext ready for unit testing
      1. 17.2.1 The application’s DbContext options are provided via its constructor
      2. 17.2.2 Setting an application’s DbContext options via OnConfiguring
    3. 17.3 Three ways to simulate the database when testing EF Core applications
    4. 17.4 Choosing between a production-type database and an SQLite in-memory database
    5. 17.5 Using a production-type database in your unit tests
      1. 17.5.1 Providing a connection string to the database to use for the unit test
      2. 17.5.2 Providing a database per test class to allow xUnit to run tests in parallel
      3. 17.5.3 Making sure that the database’s schema is up to date and the database is empty
      4. 17.5.4 Mimicking the database setup that EF Core migration would deliver
    6. 17.6 Using an SQLite in-memory database for unit testing
    7. 17.7 Stubbing or mocking an EF Core database
    8. 17.8 Unit testing a Cosmos DB database
    9. 17.9 Seeding a database with test data to test your code correctly
    10. 17.10 Solving the problem of one database access breaking another stage of your test
      1. 17.10.1 Test code using ChangeTracker.Clear in a disconnected state
      2. 17.10.2 Test code by using multiple DbContext instances in a disconnected state
    11. 17.11 Capturing the database commands sent to a database
      1. 17.11.1 Using the LogTo option extension to filter and capture EF Core logging
      2. 17.11.2 Using the ToQueryString method to show the SQL generated from a LINQ query
    12. Summary
  26. Appendix. A brief introduction to LINQ
    1. A.1 An introduction to the LINQ language
      1. A.1.1 The two ways you can write LINQ queries
      2. A.1.2 The data operations you can do with LINQ
    2. A.2 Introduction to IQueryable<T> type, and why it’s useful
      1. A.2.1 Splitting up a complex LINQ query by using the IQueryable<T> type
      2. A.2.2 How EF Core translates IQueryable<T> into database code
    3. A.3 Querying an EF Core database by using LINQ
  27. index

Product information

  • Title: Entity Framework Core in Action, Second Edition
  • Author(s): Jon Smith
  • Release date: June 2021
  • Publisher(s): Manning Publications
  • ISBN: 9781617298363