Member-only story

5 EF Core Features Every .NET Developer Should Know to Solve Common Problems

Arshak Ahamed
5 min readNov 8, 2024

EF Core code snippet demonstrating how to use compiled queries for optimizing query performance

Entity Framework Core (EF Core) is a game-changer for .NET developers who want an efficient and reliable way to interact with databases. By transforming complex SQL queries into simple C# code, EF Core takes a lot of the hassle out of working with data. But here’s the thing — many developers aren’t taking full advantage of the powerful features EF Core has to offer. Today, I’m going to share 5 EF Core features that can solve real-world developer problems and significantly improve your productivity.

These are features I wish I’d known about when I started, and I bet they’ll save you from a lot of headaches too.

1. Global Query Filters: Avoid Repetitive Filtering Logic

Problem: Are you tired of adding the same filter condition for “soft deleted” entities in every single query?

Solution: Use Global Query Filters in EF Core to automatically apply a condition across all queries for an entity. This feature is especially useful for implementing soft deletes, multi-tenancy, or any scenario where you need to consistently filter

public class AppDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.HasQueryFilter(p => !p.IsDeleted); // Automatically filter out deleted items
}
}

With this approach, EF Core will always exclude deleted products from your queries. This saves you from the risk of forgetting to add the filter in every query and makes your code cleaner.

Keywords: EF Core Global Query Filters, EF Core soft delete, database filtering

2. Shadow Properties: Track Audit Data Without Cluttering Your Models

Problem: Need to keep track of extra information like when an item was last modified, but don’t want to clutter your entity classes?

Solution: Shadow Properties allow you to store metadata without including it directly in your C# classes. They are perfect for audit data like LastModified or CreatedBy, which you may need in the database but don’t want to be part of your main domain model.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Arshak Ahamed
Arshak Ahamed

Written by Arshak Ahamed

Lead Software Engineer (Full Stack) & Tech enthusiastic

No responses yet

Write a response