Override ConfigureConventions(ModelConfigurationBuilder configurationBuilder) on your DbContext. It is a separate override from OnModelCreating and runs before it, so model building starts with your defaults: configurationBuilder.Properties<decimal>().HavePrecision(18, 2) and configurationBuilder.Properties<string>().HaveMaxLength(256) apply to every property of that type, and Properties<Money>().HaveConversion<MoneyConverter>() registers a converter once. Anything you configure explicitly in OnModelCreating or with attributes overrides the default. For rules a type filter cannot express, add a custom convention with configurationBuilder.Conventions.Add(...). Because the defaults change column types, the next migration will alter existing columns: review it for truncation, and check the snapshot in a test so a changed convention never slips through silently.
"I use pre-convention configuration: override
ConfigureConventionson theDbContext. It is not part ofOnModelCreating; EF Core calls it first, so everything I set there is the starting point for the model. Typical lines areProperties<decimal>().HavePrecision(18, 2)so no money column falls back to EF's warning-generating default, andProperties<string>().HaveMaxLength(256)so strings stop becomingnvarchar(max).The rule is: convention first, explicit configuration wins. If
Customer.Notesreally needsnvarchar(max), I say so in itsIEntityTypeConfiguration<T>. For rules a type filter can't express, such as 'every property namedconfigurationBuilder.Conventions.Add.The mistake I see is adding the defaults to a mature model without reading the migration. Turning
nvarchar(max)intonvarchar(256)is an ALTER COLUMN that fails, or truncates in a script, if longer values exist, and it can rebuild indexes on big tables. So I check the data first (MAX(LEN(col))), ship the change as its own migration, and keep a test that diffs the model so the next convention change is visible in review."
| Need | API |
|---|---|
| All decimals 18,2 | configurationBuilder.Properties<decimal>().HavePrecision(18, 2) |
| All strings bounded | configurationBuilder.Properties<string>().HaveMaxLength(256) |
| A value object everywhere | configurationBuilder.Properties<Money>().HaveConversion<MoneyConverter>() |
| Rule by name or attribute | Custom convention via configurationBuilder.Conventions.Add(_ => new MyConvention()) |
| One exception | Fluent API in OnModelCreating (overrides the default) |
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.Properties<decimal>().HavePrecision(18, 2);
configurationBuilder.Properties<string>().HaveMaxLength(256);
}
Bounded strings can be index keys (the 1,700-byte non-clustered key limit) and give the optimiser smaller row-size estimates, so memory grants shrink. The cost is a migration that alters columns; on large Azure SQL tables run it in a quiet window or online.
Adding HaveMaxLength(256) to a [40-table] model generated [120] ALTER COLUMN statements; three failed in staging because existing values were longer, which we caught by checking MAX(LEN()) before release.
Checks knowledge of pre-convention configuration.
Last updated 23 Nov 2026.