Entity Framework Core Interview Question #90

What are computed columns in EF Core?

Migrations, Database Schema & Data Management Mid-Level Intermediate

Quick Interview Answer

A computed column is calculated by the database from an expression rather than being supplied directly by ordinary application inserts and updates.

Detailed Explanation

A computed column is calculated by the database from an expression rather than being supplied directly by ordinary application inserts and updates.

EF Core can map a property to a computed SQL expression. This can be useful when a derived value must remain consistent for all database clients.

Computed-column capabilities and syntax are database-provider specific, so portability should be considered.

Code Example

modelBuilder.Entity<OrderLine>()
    .Property(x => x.LineTotal)
    .HasComputedColumnSql(
        "[Quantity] * [UnitPrice]");