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.
"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 toIdandCustomerName. 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,
_statusor 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 raiseINotifyPropertyChanged. So for DDD aggregates I use a private parameterless or binding constructor, private setters and methods likeorder.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.");
}
| 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 |
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.
Checks knowledge of constructor binding and field access.
Last updated 23 Nov 2026.