Skip to content

A narrowing cast in a lambda is left out of the template: (int)x.Amount == 2 is stored as {Amount} = 2 #103

Closed

Description

@CaffeinatedCoder

Found by the pre-preview sweep of main @ 948496a (GPT's read of the translator; reproduced in a scratch test).

What happens

pg.Exclusion(pg.Overlaps(x => x.Period), where: x => (int)x.Amount == 2, name: "ex_cast")   // Amount is a decimal

is stored as {Amount} = 2 and reaches the database as

ALTER TABLE gigs ADD CONSTRAINT ex_cast EXCLUDE USING gist ("Period" WITH &&) WHERE ("Amount" = 2);

For Amount = 2.5 the lambda is true and the predicate false: the row is outside the constraint. Nothing is refused. EF Core's own query for the same lambda keeps the cast.

Why

LambdaTranslator.Visit drops every Convert node (LambdaTranslator.cs:460), and so do the operands of a comparison (:272). That is right for the conversions the compiler adds (to Nullable<T>, to object, a widening between integer types the database compares alike) and wrong for a cast the consumer wrote that changes the value.

Change

A conversion between numeric types that can change the value is refused where the lambda is written, like the other constructs outside the tables, or written as the provider's query writes it and added to the parity sweep. Since the accepted constructs are data, the allowed conversions become a table the sweep and the analyzer (#31) read.

Test

LambdaTranslatorTests: the cast above refused or rendered; the lifts and widenings that must stay silent each as a row; the sweep's row for it.

Activity

  1. added this to the 11.0.0 milestone on Nov 14, 2026
  2. added
    bug
    silent
    package/core
    severity/medium
    area/expression-indexes
    on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/expression-indexesbugpackage/coreseverity/mediumsilent

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions