Member-only story

10 Entity Framework Core Hacks

Alex Maher

Entity Framework Core (EF Core) is a powerful, lightweight, and extensible Object-Relational Mapper (ORM) for .NET applications. It simplifies data access by abstracting the complexities of working with relational databases, allowing developers to focus on writing application logic rather than dealing with low-level SQL queries. As EF Core continues to evolve, there are numerous lesser-known features and hacks that can significantly improve the efficiency and productivity of developers working with it.

In this article, I will show you 10 amazing Entity Framework Core hacks that you might not have known about. So, let’s get started!

1. Shadow Properties

Shadow Properties are properties that are not defined in your entity class but are included in the Entity Framework Core model. They can be used to store additional information about the entity without altering the entity class itself. Shadow properties can be useful for tracking metadata like “CreatedBy,” “ModifiedBy,” or “ModifiedDate” for an entity.

Defining a Shadow Property

public class ApplicationDbContext : DbContext
{
public DbSet<Book> Books { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{…

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

Alex Maher
Alex Maher

Written by Alex Maher

.NET C# dev with 10+ yrs exp, self-taught & passionate web developer. Sharing tips & experiences in C# and web dev.

Responses (7)

How is the shadow property populated? I mean, ModifiedDate is nice but how does EF know that it's the date of the modification that's supposed to be there? Is there a convention that certain fields (by a specific names) may be added the way you…

I think the brothership of ORM anf EF is a rapid and quick solution in small-scale apps' needs.

Hi Alex, I’m not sure about your last example, EF Core will use the parameterized constructor if it finds one, without needing to use the from sql method. I think the only constraint is that the parameters need to be primitives.