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

📚 Interview question

How do you set model-wide defaults in EF Core, such as decimal precision or string length for every property?

Junior Theory Beginner

🎯 Key points

  • ConfigureConventions is its own override and runs before OnModelCreating
  • Properties<decimal>().HavePrecision(18, 2), Properties<string>().HaveMaxLength(n)
  • Properties<T>().HaveConversion<TConverter>() for value objects
  • Explicit Fluent API or attributes override the convention
  • Conventions.Add for custom IModelFinalizingConvention-style rules
  • Changing a default alters existing columns in the next migration: review for truncation
  • Smaller declared sizes help memory grants and allow indexing
  • Guard the model with a snapshot or model test

⏱ 30-second answer

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.

🗣 Full answer (senior)

How I would answer this in an interview

"I use pre-convention configuration: override ConfigureConventions on the DbContext. It is not part of OnModelCreating; EF Core calls it first, so everything I set there is the starting point for the model. Typical lines are Properties<decimal>().HavePrecision(18, 2) so no money column falls back to EF's warning-generating default, and Properties<string>().HaveMaxLength(256) so strings stop becoming nvarchar(max).

The rule is: convention first, explicit configuration wins. If Customer.Notes really needs nvarchar(max), I say so in its IEntityTypeConfiguration<T>. For rules a type filter can't express, such as 'every property named Email is 320 characters', I add a custom convention with configurationBuilder.Conventions.Add.

The mistake I see is adding the defaults to a mature model without reading the migration. Turning nvarchar(max) into nvarchar(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."


Where each kind of default goes

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);
}

Performance and schema impact

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.

Production example

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.

🔍 What the interviewer is checking

Checks knowledge of pre-convention configuration.

↩ Usually asked after

More in EF Core Fundamentals

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me