What is a shadow property in EF Core?
Quick Answer
A shadow property exists in the EF Core model and the database column but not as a property on your entity class. EF creates them automatically for conventional foreign keys, and you can add your own — e.g. audit columns like LastModified — without polluting the domain model. You read/write them via EF.Property<T>(entity, "Name") or in SaveChanges.
A shadow property is a column EF knows about and maps to the database, but that isn't a property on the C# class. EF creates them for you when a foreign key isn't declared on the entity. Where I use my own is audit fields — LastModified, CreatedBy — so the database has the column and EF tracks it, but the domain entity stays clean. I set them in one place by overriding SaveChanges and iterating the change tracker entries, and I read them with EF.Property.
- Auto-created: if
Orderhas anCustomernavigation but noCustomerIdproperty, EF adds a shadowCustomerId. - Configure:
modelBuilder.Entity<Order>().Property<DateTime>("LastModified");. - Set them centrally in an overridden
SaveChanges: loopChangeTracker.Entries()and setentry.Property("LastModified").CurrentValue = .... - Great for cross-cutting audit fields the domain shouldn't know about.