MSDN Documentation

Your source for Microsoft Developer Network content

EF Core Advanced: Data Access

Welcome to the advanced section of Entity Framework Core (EF Core) data access tutorials. This series delves into more complex scenarios and powerful features of EF Core, enabling you to build highly efficient, robust, and scalable data-driven applications.

Understanding EF Core Internals

Before diving into advanced topics, it's beneficial to have a solid understanding of how EF Core works under the hood. This includes aspects like:

  • The change tracker and its role in detecting modifications.
  • How EF Core translates LINQ queries into SQL.
  • The execution pipeline for save operations.

Advanced Querying Techniques

Mastering query capabilities is crucial for performance. This section covers:

  • Lazy Loading vs. Eager Loading: Understanding the trade-offs and how to configure them.
  • Explicit Loading: Manually loading related data when needed.
  • Projection: Selecting specific columns to reduce data transfer.
  • Executing Raw SQL: When to fall back to raw SQL queries and how to do it safely.
  • Query Tags: Adding identifiers to queries for better logging and debugging.

Consider the following example of projection:


var userSummaries = context.Users
    .Select(u => new { u.Id, u.Name, u.Email })
    .ToList();
                

Performance Optimization Strategies

Performance is often a bottleneck in data access. We'll explore key optimization strategies:

  • AsNoTracking(): Disabling change tracking for read-only queries.
  • Batch Updates and Deletes: Efficiently updating or deleting multiple records.
  • Connection Pooling: Leveraging database connection pooling for reduced overhead.
  • Query Caching: Strategies to cache query results where appropriate.
  • Index Tuning: Understanding how EF Core creates indexes and how to influence them.
Note: Using AsNoTracking() can significantly improve read performance when you don't intend to modify the retrieved entities.

Concurrency Control

Handling concurrent data modifications is essential for data integrity. This topic includes:

  • Optimistic Concurrency: Using row versions or timestamps to detect conflicts.
  • Pessimistic Concurrency: Locking records to prevent simultaneous modifications (often handled at the database level).
  • Handling Concurrency Exceptions: Implementing retry logic or user notification mechanisms.

Advanced Migrations and Schema Management

Managing database schema evolution is a critical part of application development. We'll cover:

  • Customizing Migrations: Writing custom SQL in migrations.
  • Data Seeding: Populating your database with initial data.
  • Reverting Migrations: Rolling back schema changes.
  • Managing Multiple Databases: Working with different database contexts or schemas.

Transactions and Savepoints

Ensuring atomicity of operations is vital. Learn about:

  • Using DbContext.Database.BeginTransaction(): Explicitly managing transactions.
  • Savepoints: Performing partial rollbacks within a transaction.
  • Distributed Transactions: Integrating with external transaction coordinators.

Logging and Diagnostics

Debugging and monitoring data access can be challenging. We'll explore:

  • EF Core Logging: Configuring the built-in logging to see generated SQL, query execution times, etc.
  • Diagnostic Sources: Hooking into EF Core's internal events.
  • Profiler Integration: Using external tools to profile database activity.
Tip: Enable SQL logging during development to verify that EF Core is generating the SQL queries you expect.

Customizing EF Core Behavior

For specific needs, you might need to customize EF Core's behavior:

  • Custom Conventions: Modifying default EF Core behavior for entity mapping.
  • Custom Value Converters: Handling complex data types.
  • Custom Interceptors: Intercepting operations like saving changes or executing queries.

By mastering these advanced concepts, you'll be well-equipped to tackle complex data access challenges with Entity Framework Core.