📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

How does EF Core materialise an entity that has private setters and no public parameterless constructor?

Mid-Level Theory Intermediate

🎯 Key points

  • Constructor binding matches parameters to mapped properties by name and type
  • Falls back to a parameterless constructor, which may be private
  • Remaining properties set via backing fields (default) or private setters
  • Setter logic and validation are bypassed during materialisation
  • Navigations are not constructor parameters; set by fix-up
  • Snapshot change tracking does not need public setters
  • Notification tracking (INotifyPropertyChanged) is the case that needs change events
  • UsePropertyAccessMode to control field vs property access

⏱ 30-second answer

EF Core does not need public setters or a public constructor. To create the object it uses constructor binding: it picks a constructor whose parameters match mapped scalar properties by name and type (camelCase name binds to Name), and passes the column values in. If none fits, it uses a parameterless constructor, which may be private. Any remaining mapped properties are set afterwards, through the backing field by default (_name or <Name>k__BackingField) or a private setter, so no domain logic in setters runs. Navigations can't be constructor parameters; they are fixed up after construction. This is the recommended way to keep DDD entities encapsulated, and it does not hurt change tracking: EF Core's default snapshot tracking compares values at DetectChanges, so it never relies on public setters. Only if you opt into notification tracking (INotifyPropertyChanged) must property changes raise events.

🗣 Full answer (senior)

How I would answer this in an interview

"When EF Core materialises a row it first has to construct the object. It looks for a constructor whose parameters it can bind to mapped properties: same type, name matching ignoring case, so Order(Guid id, string customerName) binds to Id and CustomerName. If there is no such constructor it uses a parameterless one, and that one can be private; EF Core calls it through compiled expression trees, not public reflection.

Then it sets whatever the constructor didn't cover. By default it writes the backing field directly, _status or the compiler-generated field of an auto-property, so private setters and even get-only properties work, and validation inside setters is not re-run when loading data that's already valid. Navigations are never constructor parameters; relationship fix-up assigns them after the object exists.

I've heard people say private setters break change tracking. They don't. EF Core's default strategy is snapshot tracking: it records original values when the entity is loaded and compares them at DetectChanges, so it doesn't care how a value changed. Only if you switch to notification-based tracking do the properties need to raise INotifyPropertyChanged. So for DDD aggregates I use a private parameterless or binding constructor, private setters and methods like order.Ship()."


public sealed class Order
{
    private readonly List<OrderLine> _lines = [];

    private Order() { } // used by EF Core only

    public Order(Guid id, string customerName) => (Id, CustomerName) = (id, customerName);

    public Guid Id { get; private set; }
    public string CustomerName { get; private set; } = "";
    public OrderStatus Status { get; private set; }
    public IReadOnlyCollection<OrderLine> Lines => _lines;

    public void Ship() => Status = Status == OrderStatus.Paid
        ? OrderStatus.Shipped
        : throw new InvalidOperationException("Only paid orders ship.");
}

Rules

Situation What EF Core does
Constructor with matching parameters Binds column values into it
Only a private parameterless constructor Uses it
Get-only auto-property Writes the backing field
Lines exposed as read-only, _lines field Discovered by convention, or Navigation(o => o.Lines).HasField("_lines")
INotifyPropertyChanged tracking Setters must raise events

Production example

Moving an aggregate from public setters to private setters plus a private constructor needed [no] mapping changes beyond one HasField for the lines collection, and removed [a class of bugs] where handlers changed Status directly.

🔍 What the interviewer is checking

Checks knowledge of constructor binding and field access.

↩ Usually asked after

More in EF Core Fundamentals

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me