Book description
Entity Framework Core in Action teaches you how to access and update relational data from .NET applications. Following the crystal-clear explanations, real-world examples, and around 100 diagrams, you’ll discover time-saving patterns and best practices for security, performance tuning, and unit testing.
About the Technology
There’s a mismatch in the way OO programs and relational databases represent data. Entity Framework is an object-relational mapper (ORM) that bridges this gap, making it radically easier to query and write to databases from a .NET application. EF creates a data model that matches the structure of your OO code so you can query and write to your database using standard LINQ commands. It will even automatically generate the model from your database schema.
About the Book
Using crystal-clear explanations, real-world examples, and around 100 diagrams, Entity Framework Core in Action teaches you how to access and update relational data from .NET applications. You’ll start with a clear breakdown of Entity Framework, long with the mental model behind ORM. Then you’ll discover time-saving patterns and best practices for security, performance tuning, and even unit testing. As you go, you’ll address common data access challenges and learn how to handle them with Entity Framework.
What's Inside
- Querying a relational database with LINQ
- Using EF Core in business logic
- Integrating EF with existing C# applications
- Applying domain-driven design to EF Core
- Getting the best performance out of EF Core
- Covers EF Core 2.0 and 2.1
About the Reader
For .NET developers with some awareness of how relational databases work.
About the Author
Jon P Smith is a full-stack developer with special focus on .NET Core and Azure.
Quotes
An expertly written guide to EF Core—quite possibly the only reference you’ll ever need.
- Stephen Byrne, Action Point
A solid book that deals well with the topic at hand, but also handles the wider concerns around using EF in real-world applications.
- Sebastian Rogers, Simple Innovations
This is the next step beyond the basics. It’ll help you get to the next level!
- Jeff Smith, Agilify Automation
Great book with excellent, real-world examples.
- Tanya Wilke, Sanlam
Publisher resources
Table of contents
- Titlepage
- Copyright
- preface
- acknowledgments
- about this book
- about the author
- about the cover illustration
-
Part 1: Getting started
-
Chapter 1: Introduction to Entity FrameworkCore
- 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 Should you use EF Core in your next project?
- 1.11 When should you not use EF Core?
- Summary
-
Chapter 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: moving part of your query into software
- 2.6 Building complex queries
- 2.7 Adding sorting, filtering, and paging
- 2.8 Putting it all together: combining query objects
- Summary
-
Chapter 3: Changing the database content
- 3.1 Introducing EF Core’s entity State
- 3.2 Creating new rows in a table
- 3.3 Updating database rows
-
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
- Summary
-
Chapter 4: Using EF Core in business logic
- 4.1 Why is business logic so different from other code?
- 4.2 Our business need—processing an order for books
- 4.3 Using a design pattern to help implement 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 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 Any disadvantages of this business logic pattern?
- 4.5 Placing an order on the book app
- 4.6 Adding extra features to your business logic handling
- Summary
-
Chapter 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 Migrate to change the database structure
- 5.10 Using async/await for better scalability
- 5.11 Running parallel tasks: how to provide the DbContext
- Summary
-
Chapter 1: Introduction to Entity FrameworkCore
-
Part 2: Entity Framework in depth
-
Chapter 6: Configuring nonrelational properties
- 6.1 Three ways of configuring EF Core
- 6.2 A worked example of configuring EF Core
- 6.3 Configuring By Convention
- 6.4 Configuring via Data Annotations
- 6.5 Configuring via the Fluent API
- 6.6 Excluding properties and classes from the database
- 6.7 Configuring model-level query filters
- 6.8 Setting database column type, size, and nullability
- 6.9 The different ways of configuring the primary key
- 6.10 Adding indexes to database columns
- 6.11 Configuring the naming on the database side
- 6.12 Using specific database-provider Fluent API commands
- 6.13 Recommendations for using EF Core’s configuration
- 6.14 Shadow properties—hide column data inside EF Core
- 6.15 Backing fields—controlling access to data in an entity class
- Summary
-
Chapter 7: Configuring relationships
- 7.1 Defining some relationship terms
- 7.2 What navigational properties do you need?
- 7.3 Configuring relationships
-
7.4 Configuring relationships By Convention
- 7.4.1 What makes a class an entity class?
- 7.4.2 An example of an entity class with navigational properties
- 7.4.3 How EF Core finds foreign keys By Convention
- 7.4.4 Nullability of foreign keys—required or optional relationships
- 7.4.5 Foreign keys—what happens if you leave them out?
- 7.4.6 When does By Convention configuration not work?
- 7.5 Configuring relationships by using Data Annotations
- 7.6 Fluent API relationship configuration commands
- 7.7 Additional methods available in Fluent API relationships
- 7.8 Alternative ways of mapping entities to database tables
- Summary
-
Chapter 8: Configuring advanced features and handling concurrency conflicts
- 8.1 Advanced feature—using backing fields with relationships
- 8.2 DbFunction—using user-defined functions with EF Core
- 8.3 Computed column—a dynamically calculated column value
- 8.4 Setting a default value for a database column
- 8.5 Sequences—providing numbers in a strict order
- 8.6 Marking database-generated properties
- 8.7 Handling simultaneous updates—concurrency conflicts
- Summary
-
Chapter 9: Going deeper into the DbContext
- 9.1 Overview of the DbContext class’s properties
- 9.2 Understanding how EF Core tracks changes
-
9.3 Details on every command that changes an entity’s State
- 9.3.1 The Add command--inserting a new row in the database
- 9.3.2 The Remove command—deleting a row from the database
- 9.3.3 Modifying a tracked entity—EF Core’s DetectChanges
- 9.3.4 INotifyPropertyChanged entities—a different way of tracking changes
- 9.3.5 The Update method—telling EF Core that everything has changed
- 9.3.6 The Attach method—changing an untracked entity into a tracked entity
- 9.3.7 Setting the State of an entity directly
- 9.3.8 TrackGraph—handling disconnected updates with relationships
- 9.4 Using ChangeTracker to detect changes
- 9.5 Using raw SQL commands in EF Core
- 9.6 Using Context.Model to access EF Core’s view of the database
- 9.7 Handling database connection problems
- Summary
-
Chapter 6: Configuring nonrelational properties
-
Part 3: Using Entity Framework Core in real-world applications
-
Chapter 10: Useful software patterns for EF Core applications
- 10.1 Another look at the separation-of-concerns principle
- 10.2 Using patterns to speed development of database access
- 10.3 Speed up query development—use a LINQ mapper
- 10.4 Domain-driven-design database repository
- 10.5 Is the Repository pattern useful with Entity Framework?
- 10.6 Splitting a database across multiple DbContexts
- 10.7 Data validation and error-handling patterns
- Summary
- Chapter 11: Handling database migrations
-
Chapter 12: EF Core performance tuning
- 12.1 Part 1—Deciding which performance issues to fix
- 12.2 Part 2—Techniques for diagnosing a performance issue
- 12.3 Part 3—Techniques for fixing performance issues
-
12.4 Using good patterns makes your application perform well
- 12.4.1 Using Select loading to load only the columns you need
- 12.4.2 Using paging and/or filtering of searches to reduce the rows you load
- 12.4.3 A warning that using lazy loading will affect database performance
- 12.4.4 Always adding the AsNoTracking method to read-only queries
- 12.4.5 Using the async version of EF Core commands to improve scalability
- 12.4.6 Ensuring that your database access code is isolated/decoupled
-
12.5 Performance antipatterns—database access
- 12.5.1 Not minimizing the number of calls to the database
- 12.5.2 Calling SaveChanges multiple times
- 12.5.3 Allowing too much of a data query to be moved into the software side
- 12.5.4 Not replacing suboptimal SQL translations with user-defined functions
- 12.5.5 Not precompiling queries that are used frequently
- 12.5.6 Expecting EF Core to build the best SQL database commands
- 12.5.7 Not using the Find method when an entity might be already loaded
- 12.5.8 Missing indexes from a property that you want to search on
- 12.5.9 Mismatching column data types
- 12.6 Performance antipatterns—software
- 12.7 Performance patterns—scalability of database accesses
- Summary
-
Chapter 13: A worked example of performance tuning
- 13.1 Part 1a—Making sure a single query performs well
- 13.2 Part 1b—Improving the query by adding a DbFunction
- 13.3 Part 2—Converting EF Core commands to SQL queries
- 13.4 Part 3—Modifying the database to increase performance
- 13.5 Comparing parts 1a, 1b, 2, and 3
- 13.6 Database scalability—what can you do to improve that?
- Summary
-
Chapter 14: Different database types and EF Core services
- 14.1 What differences do other database server types bring?
-
14.2 Developing a CQRS architecture application with EF Core
- 14.2.1 Implementation of a two-database CQRS architecture application
- 14.2.2 How the parts of the CQRS solution interact with each other
- 14.2.3 Finding book view changes—Part 1, finding the correct state and key
- 14.2.4 Finding the book view changes—Part 2, building the correct State
- 14.2.5 Why the CQRS solution is less likely to have out-of-date cached values
- 14.2.6 Is the two-database CQRS architecture worth the effort?
- 14.3 Accessing and changing EF Core services
- 14.4 Accessing command-line tools from software
- Summary
- Chapter 15: Unit testing EF Core applications
- 15.2 Simulating the database when testing EF Core applications
- 15.3 Getting your application’s DbContext ready for unit testing
- 15.4 Simulating a database—using an in-memory database
- 15.5 Using a real database in your unit tests
- 15.6 Unit testing a disconnected state update properly
- 15.7 Mocking a database repository pattern
- 15.8 Capturing EF Core logging information in unit testing
- 15.9 Using the EfSchemaCompare tool in your unit tests
- Summary
-
Chapter 10: Useful software patterns for EF Core applications
- appendix A: A brief introduction to LINQ
-
appendix B: Early information on EF Core version 2.1
- B.1 What does the 2.1 in the EF Core release number mean?
-
B.2 Brand-new features
- B.2.1 Lazy loading—loading relationships when you need them
- B.2.2 Parameters in entity class constructors
- B.2.3 Value conversion—defining the mapping of value types to the database
- B.2.4 Data seeding—adding initial data to a new/updated database
- B.2.5 Query types—using non-entity classes in read-only queries
- B.2.6 Include derived types when using table per hierarchy
- B.2.7 Ability to link to entity class state change events
- B.2.8 Supporting NoSQL—Cosmos NoSQL Database provider (preview)
-
B.3 Improvements to existing features
- B.3.1 LINQ GroupBy translation to SQL GROUP BY command
- B.3.2 Optimization of correlated subqueries—the N + 1 SQL query problem
- B.3.3 .NET Core global tools—installing design-time tools locally
- B.3.4 Column ordering in database now follows entity-class property order
- B.3.5 System.Transactions support
- B.3.6 Specifying an owned type via an attribute
- Summary
- Index
Product information
- Title: Entity Framework Core in Action
- Author(s):
- Release date: August 2018
- Publisher(s): Manning Publications
- ISBN: 9781617294563
You might also like
video
Entity Framework Core - A Full Tour
We begin with an introduction to Entity Framework Core, followed by an essential update on .NET …
book
Entity Framework Core in Action, Second Edition
Entity Framework Core in Action, Second Edition is an in-depth guide to reading and writing databases …
video
Learning Entity Framework Core
Entity Framework is a highly recommended Object Relation Mapping tool used to build complex systems. This …
book
ASP.NET Core in Action
ASP.NET Core in Action is for C# developers without any web development experience who want to …