Skip to content

Native AOT compiled models (dbcontext optimize --nativeaot) fail: plugin type mappings have no public static Default #209

Open

Description

@CaffeinatedCoder

Generating a Native AOT compiled model fails for any model with a range, multirange or value set property. That form is what dotnet ef dbcontext optimize --nativeaot writes, and --precompile-queries needs it. It fails loudly before any code is written:

System.InvalidOperationException: The type mapping used is incompatible with a compiled model.
The mapping type must have a 'public static readonly ValueRangeTypeMapping<DateRange, DateOnly> ValueRangeTypeMapping<DateRange, DateOnly>.Default' property.

Measured on main (d073eb6) with the in-process generator CompiledModelJsonTests runs (#43), setting CompiledModelCodeGenerationOptions.ForNativeAot = true. The standard compiled model (dotnet ef dbcontext optimize) works: it carries no type mappings and resolves them through the plugin at run time. #43's test covers every discovered type in that form.

What this means for the JSON reader/writers. LiteralJsonValueReaderWriter and the three YearMonth reader/writers are public, with ConstructorExpression pointing at a static Instance, "because a compiled model re-creates the reader/writer through ConstructorExpression". Only the Native AOT form reads ConstructorExpression, and that form can't be generated. So the design has never run. Both seeds below pass every suite today, including #43's test, because the standard form never consults them:

  • ConstructorExpression => Expression.New(<the private constructor>). Generated code could not construct the reader/writer (CS0122).
  • YearMonthRangeJsonValueReaderWriter.InstanceProperty pointing at LiteralJsonValueReaderWriter<YearMonthRange>.Instance. A Native AOT model would write YearMonthRange as its own literal where the runtime model writes the month-aligned daterange.

Options.

  1. Give every plugin type mapping (ValueRangeTypeMapping, ValueRangeSetTypeMapping, ValueSetTypeMapping, the bridged element mappings) the Default EF Core needs, and whatever else the Native AOT generator asks for next. Then run Test JSON-column readers and writers through a compiled model #43's test with ForNativeAot = true and register it as an oracle with the two seeds above.
  2. Document that the EF Core packages support the standard compiled model but not the Native AOT one. That fits the EF packages not claiming AOT compatibility (PackagingConventionTests). The Instance/ConstructorExpression comments would then say they are for the Native AOT form.

Not silent: generation throws.

Activity

  1. added this to the 10.0.0 milestone on Oct 18, 2026
  2. added
    bug
    package/efcore
    area/aot
    package/efcore-nodatime
    severity/low
    on Oct 18, 2026
  3. removed this from the 10.0.0 milestone on Oct 18, 2026
  4. CaffeinatedCoder commented on Oct 18, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Moved to the backlog (#83). Additions are frozen for 10.0.0 (epic #58, 2026-10-18), and this fails loudly at design time, so it can ship in a 10.x minor within the additions window (#178). It supersedes #42, which asked for the same static Default without the measurement; #42 is closed as a duplicate.

  5. CaffeinatedCoder commented on Oct 18, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Related but separate: #47 now designs CodoMetis.ValueRanges.Npgsql, an AOT-safe path through raw Npgsql that doesn't need EF Core or this issue.

  6. CaffeinatedCoder commented on Oct 18, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Takes over the obligation #43's test can't hold (pre-tag analysis #179): the ConstructorExpression of the JSON reader/writers is consulted only by --nativeaot compiled models. When this is fixed, its test must fail with LiteralJsonValueReaderWriter.ConstructorExpression => throw new NotSupportedException() seeded.

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/aotbugpackage/efcorepackage/efcore-nodatimeseverity/low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions