5 Key EF Core Features Every Developer Should Master

minhduc159

Duc Dang

Posted on August 20, 2024

5 Key EF Core Features Every Developer Should Master

Entity Framework Core (EF Core) is a powerful Object-Relational Mapper (ORM) for .NET developers. Mastering its key features can significantly enhance your productivity and the performance of your applications. In this post, we will explore five essential EF Core features that every developer should master, complete with examples.

1. Query Splitting

Query Splitting is a feature that helps optimize the performance of your queries by splitting a single query into multiple queries. This can be particularly useful when dealing with large datasets and complex queries.

Example

Here's how you can use Query Splitting in EF Core:

var orders = context.Orders
    .Include(o => o.OrderItems)
    .AsSplitQuery()
    .ToList();
Enter fullscreen mode Exit fullscreen mode

In this example, the AsSplitQuery method splits the query into multiple queries, reducing the load on the database and improving performance.

2. Bulk Updates and Deletes

EF Core allows you to perform bulk updates and deletes efficiently. This feature is essential when you need to update or delete a large number of records without loading them into memory.

Example

Here's how you can perform a bulk update and delete in EF Core:

// Bulk Update
context.Customers
    .Where(c => c.IsActive)
    .ExecuteUpdate(c => c.SetProperty(c => c.IsActive, false));

// Bulk Delete
context.Customers
    .Where(c => !c.IsActive)
    .ExecuteDelete();
Enter fullscreen mode Exit fullscreen mode

These methods allow you to update or delete multiple records in a single database operation, improving performance and reducing memory usage.

3. Raw SQL Queries

EF Core allows you to execute raw SQL queries directly against the database. This feature is useful when you need to perform complex queries that are not easily expressed using LINQ.

Example

Here's how you can execute a raw SQL query in EF Core:

var customers = context.Customers
    .FromSqlRaw("SELECT * FROM Customers WHERE IsActive = 1")
    .ToList();
Enter fullscreen mode Exit fullscreen mode

This method allows you to leverage the full power of SQL while still benefiting from EF Core's change tracking and other features.

4. Query Filters

Query Filters allow you to define filters that apply to all queries for a given entity type. This is useful for implementing multi-tenancy, soft deletes, and other scenarios.

Example

Here's how you can define a global query filter for soft deletes:

public class AppDbContext : DbContext
{
    public DbSet<Customer> Customers { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Customer>().HasQueryFilter(c => !c.IsDeleted);
    }
}
Enter fullscreen mode Exit fullscreen mode

With this filter in place, all queries for the Customer entity will automatically exclude deleted records.

5. Eager Loading

Eager Loading is a feature that allows you to load related data as part of the initial query. This can improve performance by reducing the number of database queries.

Example

Here's how you can use Eager Loading in EF Core:

var customers = context.Customers
    .Include(c => c.Orders)
    .ToList();
Enter fullscreen mode Exit fullscreen mode

In this example, the Include method ensures that the related Orders data is loaded along with the Customers data, reducing the number of queries and improving performance.

Conclusion

Mastering these key EF Core features can greatly enhance your development workflow and the performance of your applications. By leveraging Query Splitting, Bulk Updates and Deletes, Raw SQL Queries, Query Filters, and Eager Loading, you can build robust and efficient data-driven applications.

💖 💪 🙅 🚩
minhduc159
Duc Dang

Posted on August 20, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related