0%

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