Book description
Entity Framework Core in Action, Second Edition is an in-depth guide to reading and writing databases with EF Core. Revised from the bestselling original edition, it’s filled with over 100 diagrams, code snippets, and examples—including building and scaling your own bookselling web application. Learn from author Jon Smith’s extensive experience working with EF Core in production, as you discover time-saving patterns and best practices for security, performance tuning, and unit testing. All of the book’s code is available on GitHub.About the Technology
Entity Framework radically simplifies data access in .NET applications. This easy-to-use object-relational mapper (ORM) lets you write database code in pure C#. It automatically maps classes to database tables and enables queries with standard LINQ commands. It even generates SQL, so you don’t have to!
About the Book
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.
What's Inside
- Configuring EF to define every table and column
- Updating your schema as your app grows
- Integrating EF with existing C# application
- Writing and testing business logic for database access
- Applying a Domain-Driven Design to EF Core
- Getting the best performance out of EF Core
About the Reader
For .NET developers familiar with relational databases.
About the Author
Jon P. Smith is a freelance software developer and architect with a special focus on .NET and Azure.
Quotes
The most comprehensive reference for EF Core that does—or ever will—exist.
- Stephen Byrne, Intel Corporation
The definitive guide to EF Core. Filled with real world examples, it’s the most practical way to enhance your EF Core skill set.
- Paul Brown, Diversified Services Network
I firmly believe anyone using EF Core will find something to up their game in this book.
- Anne Epstein, Headspring
Remains a go-to resource for me while working with Entity Framework.
- Foster Haines, J2 Interactive
Publisher resources
Table of contents
- Entity Framework Core in Action
- Copyright
- Praise for the first edition
- contents
- front matter
- Part 1 Getting started
-
1 Introduction to Entity Framework Core
- 1.1 What you’ll learn from this book
- 1.2 My “lightbulb moment” with Entity Framework
- 1.3 Some words for existing EF6.x developers
- 1.4 An overview of EF Core
- 1.5 What about NoSQL?
- 1.6 Your first EF Core application
- 1.7 The database that MyFirstEfCoreApp will access
- 1.8 Setting up the MyFirstEfCoreApp application
- 1.9 Looking under the hood of EF Core
- 1.10 The stages of development of EF Core
- 1.11 Should you use EF Core in your next project?
- 1.12 When should you not use EF Core?
- Summary
-
2 Querying the database
- 2.1 Setting the scene: Our book-selling site
- 2.2 Creating the application’s DbContext
- 2.3 Understanding database queries
- 2.4 Loading related data
- 2.5 Using client vs. server evaluation: Adapting data at the last stage of a query
- 2.6 Building complex queries
- 2.7 Introducing the architecture of the Book App
- 2.8 Adding sorting, filtering, and paging
- 2.9 Putting it all together: Combining Query Objects
- 3 Changing the database content
-
4 Using EF Core in business logic
- 4.1 The questions to ask and the decisions you need to make before you start coding
- 4.2 Complex business logic example: Processing an order for books
- 4.3 Using a design pattern to implement complex business logic
-
4.4 Implementing the business logic for processing an order
- 4.4.1 Guideline 1: Business logic has first call on defining the database structure
- 4.4.2 Guideline 2: Business logic should have no distractions
- 4.4.3 Guideline 3: Business logic should think that it’s working on in-memory data
- 4.4.4 Guideline 4: Isolate the database access code into a separate project
- 4.4.5 Guideline 5: Business logic shouldn’t call EF Core’s SaveChanges
- 4.4.6 Putting it all together: Calling the order-processing business logic
- 4.4.7 Placing an order in the Book App
- 4.4.8 The pros and cons of the complex business logic pattern
- 4.5 Simple business logic example: ChangePriceOfferService
- 4.6 Validation business logic example: Adding review to a book, with checks
- 4.7 Adding extra features to your business logic handling
- Summary
-
5 Using EF Core in ASP.NET Core web applications
- 5.1 Introducing ASP.NET Core
- 5.2 Understanding the architecture of the Book App
- 5.3 Understanding dependency injection
- 5.4 Making the application’s DbContext available via DI
- 5.5 Calling your database access code from ASP.NET Core
- 5.6 Implementing the book list query page
- 5.7 Implementing your database methods as a DI service
- 5.8 Deploying an ASP.NET Core application with a database
- 5.9 Using EF Core’s migration feature to change the database’s structure
- 5.10 Using async/await for better scalability
- 5.11 Running parallel tasks: How to provide the DbContext
- Summary
-
6 Tips and techniques for reading and writing with EF Core
-
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 Making loading navigational collections fail-safe
- 6.1.6 Using Global Query Filters in real-world situations
- 6.1.7 Considering LINQ commands that need special attention
- 6.1.8 Using AutoMapper to automate building Select queries
- 6.1.9 Evaluating how EF Core creates an entity class when reading data in
- 6.2 Writing to the database with EF Core
- Summary
-
6.1 Reading from the database
- Part 2 Entity Framework in depth
-
7 Configuring nonrelational properties
- 7.1 Three ways of configuring EF Core
- 7.2 A worked example of configuring EF Core
- 7.3 Configuring by convention
- 7.4 Configuring via Data Annotations
- 7.5 Configuring via the Fluent API
- 7.6 Excluding properties and classes from the database
- 7.7 Setting database column type, size, and nullability
- 7.8 Value conversions: Changing data to/from the database
- 7.9 The different ways of configuring the primary key
- 7.10 Adding indexes to database columns
- 7.11 Configuring the naming on the database side
- 7.12 Configuring Global Query Filters
- 7.13 Applying Fluent API commands based on the database provider type
- 7.14 Shadow properties: Hiding column data inside EF Core
- 7.15 Backing fields: Controlling access to data in an entity class
- 7.16 Recommendations for using EF Core’s configuration
- Summary
-
8 Configuring relationships
- 8.1 Defining some relationship terms
- 8.2 What navigational properties do you need?
- 8.3 Configuring relationships
-
8.4 Configuring relationships By Convention
- 8.4.1 What makes a class an entity class?
- 8.4.2 An example of an entity class with navigational properties
- 8.4.3 How EF Core finds foreign keys By Convention
- 8.4.4 Nullability of foreign keys: Required or optional dependent relationships
- 8.4.5 Foreign keys: What happens if you leave them out?
- 8.4.6 When does By Convention configuration not work?
- 8.5 Configuring relationships by using Data Annotations
- 8.6 Fluent API relationship configuration commands
- 8.7 Controlling updates to collection navigational properties
- 8.8 Additional methods available in Fluent API relationships
-
8.9 Alternative ways of mapping entities to database tables
- 8.9.1 Owned types: Adding a normal class into an entity class
- 8.9.2 Table per hierarchy (TPH): Placing inherited classes into one table
- 8.9.3 Table per Type (TPT): Each class has its own table
- 8.9.4 Table splitting: Mapping multiple entity classes to the same table
- 8.9.5 Property bag: Using a dictionary as an entity class
- Summary
-
9 Handling database migrations
- 9.1 How this chapter is organized
- 9.2 Understanding the complexities of changing your application’s database
- 9.3 Part 1: Introducing the three approaches to creating a migration
- 9.4 Creating a migration by using EF Core’s add migration command
- 9.5 Editing an EF Core migration to handle complex situations
- 9.6 Using SQL scripts to build migrations
- 9.7 Using EF Core’s reverse-engineering tool
- 9.8 Part 2: Applying your migrations to a database
- 9.9 Migrating a database while the application is running
- Summary
-
10 Configuring advanced features and handling concurrency conflicts
- 10.1 DbFunction: Using user-defined functions (UDFs) with EF Core
- 10.2 Computed column: A dynamically calculated column value
- 10.3 Setting a default value for a database column
- 10.4 Sequences: Providing numbers in a strict order
- 10.5 Marking database-generated properties
- 10.6 Handling simultaneous updates: Concurrency conflicts
- Summary
-
11 Going deeper into the DbContext
- 11.1 Overview of the DbContext class’s properties
- 11.2 Understanding how EF Core tracks changes
-
11.3 Looking at commands that change an entity’s State
- 11.3.1 The Add command: Inserting a new row into the database
- 11.3.2 The Remove method: Deleting a row from the database
- 11.3.3 Modifying an entity class by changing the data in that entity class
- 11.3.4 Modifying an entity class by calling the Update method
- 11.3.5 The Attach method: Start tracking an existing untracked entity class
- 11.3.6 Setting the State of an entity directly
- 11.3.7 TrackGraph: Handling disconnected updates with relationships
-
11.4 SaveChanges and its use of ChangeTracker.DetectChanges
- 11.4.1 How SaveChanges finds all the State changes
- 11.4.2 What to do if ChangeTracker.DetectChanges is taking too long
- 11.4.3 Using the entities’ State within the SaveChanges method
- 11.4.4 Catching entity class’s State changes via events
- 11.4.5 Triggering events when SaveChanges/SaveChangesAsync is called
- 11.4.6 EF Core interceptors
-
11.5 Using SQL commands in an EF Core application
- 11.5.1 FromSqlRaw/FromSqlInterpolated: Using SQL in an EF Core query
- 11.5.2 ExecuteSqlRaw/ExecuteSqlInterpolated: Executing a nonquery command
- 11.5.3 AsSqlQuery Fluent API method: Mapping entity classes to queries
- 11.5.4 Reload: Used after ExecuteSql commands
- 11.5.5 GetDbConnection: Running your own SQL commands
- 11.6 Accessing information about the entity classes and database tables
- 11.7 Dynamically changing the DbContext’s connection string
- 11.8 Handling database connection problems
- Summary
- Part 3 Using Entity Framework Core in real-world applications
-
12 Using entity events to solve business problems
- 12.1 Using events to solve business problems
- 12.2 Defining where domain events and integration events are useful
- 12.3 Where might you use events with EF Core?
-
12.4 Implementing a domain event system with EF Core
- 12.4.1 Create some domain events classes to be triggered
- 12.4.2 Add code to the entity classes to hold the domain events
- 12.4.3 Alter the entity class to detect a change to trigger an event on
- 12.4.4 Create event handlers that are matched to the domain events
- 12.4.5 Build an Event Runner that finds and runs the correct event handler
- 12.4.6 Override SaveChanges and insert the Event Runner before SaveChanges is called
- 12.4.7 Register the Event Runner and all the event handlers
- 12.5 Implementing an integration event system with EF Core
- 12.6 Improving the domain event and integration event implementations
- Summary
-
13 Domain-Driven Design and other architectural approaches
- 13.1 A good software architecture makes it easier to build and maintain your application
- 13.2 The Book App’s evolving architecture
- 13.3 Introduction to DDD at the entity class level
-
13.4 Altering the Book App entities to follow the DDD approach
- 13.4.1 Changing the properties in the Book entity to read-only
- 13.4.2 Updating the Book entity properties via methods in the entity class
- 13.4.3 Controlling how the Book entity is created
- 13.4.4 Understanding the differences between an entity and a value object
- 13.4.5 Minimizing the relationships between entity classes
- 13.4.6 Grouping entity classes
- 13.4.7 Deciding when the business logic shouldn’t be run inside an entity
- 13.4.8 Applying DDD’s bounded context to your application’s DbContext
-
13.5 Using your DDD-styled entity classes in your application
- 13.5.1 Calling the AddPromotion access method via a repository pattern
- 13.5.2 Calling the AddPromotion access method via a class-to-method-call library
- 13.5.3 Adding a Review to the Book entity class via a repository pattern
- 13.5.4 Adding a Review to the Book entity class via a class-to-method-call library
- 13.6 The downside of DDD entities: Too many access methods
- 13.7 Getting around performance issues in DDD-styled entities
- 13.8 Three architectural approaches: Did they work?
- Summary
-
14 EF Core performance tuning
- 14.1 Part 1: Deciding which performance issues to fix
- 14.2 Part 2: Techniques for diagnosing a performance issue
- 14.3 Part 3: Techniques for fixing performance issues
-
14.4 Using good patterns makes your application perform well
- 14.4.1 Using Select loading to load only the columns you need
- 14.4.2 Using paging and/or filtering of searches to reduce the rows you load
- 14.4.3 Warning: Lazy loading will affect database performance
- 14.4.4 Always adding the AsNoTracking method to read-only queries
- 14.4.5 Using the async version of EF Core commands to improve scalability
- 14.4.6 Ensuring that your database access code is isolated/decoupled
-
14.5 Performance antipatterns: Database queries
- 14.5.1 Antipattern: Not minimizing the number of calls to the database
- 14.5.2 Antipattern: Missing indexes from a property that you want to search on
- 14.5.3 Antipattern: Not using the fastest way to load a single entity
- 14.5.4 Antipattern: Allowing too much of a data query to be moved into the software side
- 14.5.5 Antipattern: Not moving calculations into the database
- 14.5.6 Antipattern: Not replacing suboptimal SQL in a LINQ query
- 14.5.7 Antipattern: Not precompiling frequently used queries
-
14.6 Performance antipatterns: Writes
- 14.6.1 Antipattern: Calling SaveChanges multiple times
- 14.6.2 Antipattern: Making DetectChanges work too hard
- 14.6.3 Antipattern: Not using HashSet<T> for navigational collection properties
- 14.6.4 Antipattern: Using the Update method when you want to change only part of the entity
- 14.6.5 Antipattern: Startup issue—Using one large DbContext
-
14.7 Performance patterns: Scalability of database accesses
- 14.7.1 Using pooling to reduce the cost of a new application’s DbContext
- 14.7.2 Adding scalability with little effect on overall speed
- 14.7.3 Helping your database scalability by making your queries simple
- 14.7.4 Scaling up the database server
- 14.7.5 Picking the right architecture for applications that need high scalability
- Summary
-
15 Master class on performance-tuning database queries
- 15.1 The test setup and a summary of the four performance approaches
- 15.2 Good LINQ approach: Using an EF Core Select query
- 15.3 LINQ+UDFs approach: Adding some SQL to your LINQ code
- 15.4 SQL+Dapper: Creating your own SQL
- 15.5 LINQ+caching approach: Precalculating costly query parts
- 15.6 Comparing the four performance approaches with development effort
- 15.7 Improving database scalability
- Summary
-
16 Cosmos DB, CQRS, and other database types
- 16.1 The differences between relational and NoSQL databases
- 16.2 Introduction to Cosmos DB and its EF Core provider
- 16.3 Building a Command and Query Responsibility Segregation (CQRS) system using Cosmos DB
-
16.4 The design of a two-database CQRS architecture application
- 16.4.1 Creating an event to trigger when the SQL Book entity changes
- 16.4.2 Adding events to the Book entity send integration events
- 16.4.3 Using the EfCore.GenericEventRunner to override your BookDbContext
- 16.4.4 Creating the Cosmos entity classes and DbContext
- 16.4.5 Creating the Cosmos event handlers
- 16.5 Understanding the structure and data of a Cosmos DB account
- 16.6 Displaying books via Cosmos DB
- 16.7 Was using Cosmos DB worth the effort? Yes!
- 16.8 Differences in other database types
- Summary
-
17 Unit testing EF Core applications
- 17.1 An introduction to the unit test setup
- 17.2 Getting your application’s DbContext ready for unit testing
- 17.3 Three ways to simulate the database when testing EF Core applications
- 17.4 Choosing between a production-type database and an SQLite in-memory database
-
17.5 Using a production-type database in your unit tests
- 17.5.1 Providing a connection string to the database to use for the unit test
- 17.5.2 Providing a database per test class to allow xUnit to run tests in parallel
- 17.5.3 Making sure that the database’s schema is up to date and the database is empty
- 17.5.4 Mimicking the database setup that EF Core migration would deliver
- 17.6 Using an SQLite in-memory database for unit testing
- 17.7 Stubbing or mocking an EF Core database
- 17.8 Unit testing a Cosmos DB database
- 17.9 Seeding a database with test data to test your code correctly
- 17.10 Solving the problem of one database access breaking another stage of your test
- 17.11 Capturing the database commands sent to a database
- Summary
- Appendix. A brief introduction to LINQ
- index
Product information
- Title: Entity Framework Core in Action, Second Edition
- Author(s):
- Release date: June 2021
- Publisher(s): Manning Publications
- ISBN: 9781617298363
You might also like
book
Entity Framework Core in Action
Entity Framework Core in Action teaches you how to access and update relational data from .NET …
video
Entity Framework Core - A Full Tour
We begin with an introduction to Entity Framework Core, followed by an essential update on .NET …
book
Practical Entity Framework Core 6: Database Access for Enterprise Applications
Take a developer journey that paves the way to enterprise-level database access from .NET and C# …
book
ASP.NET Core in Action, Second Edition
Fully updated to ASP.NET 5.0, ASP.NET Core in Action, Second Edition is a hands-on primer to …