Entity Framework Core Interview Question #171

How can private properties or fields be mapped in EF Core?

Advanced Mapping, Interceptors, Value Conversions & Architecture Senior Advanced

Quick Interview Answer

EF Core can map non-public members when they are part of the model configuration. This lets domain entities protect invariants while persistence still stores required values. Mapping private state should remain understandable to maintainers and should not make ordinary querying or migration behavior unnecessarily obscure.

Detailed Explanation

EF Core can map non-public members when they are part of the model configuration. This lets domain entities protect invariants while persistence still stores required values. Mapping private state should remain understandable to maintainers and should not make ordinary querying or migration behavior unnecessarily obscure.

Code Example

modelBuilder.Entity<Customer>()
    .Property<string>("_normalizedEmail")
    .HasColumnName("NormalizedEmail");