Skip to content

Precompiled queries code produces nullable annotation warning on build #37996

Description

@donpanl

Bug description

Precompiled query code can have nullable annotations:

public static TSource? Query1_FirstOrDefault1<TSource>(this global::System.Linq.IQueryable<TSource> source)

Building it leads to a warning:

/workspaces/ef-play/nullwarn/Generated/Program.EFInterceptors.Context.cs(32,30): warning CS8669: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.

It seems the only way to ignore this warning is to disable it for the whole project.

See attached project with minimal example.

Can be reproduced with:

dotnet ef dbcontext optimize --output-dir Generated --precompile-queries --nativeaot --verbose
> dotnet build

nullwarn.zip

Your code

--- Model.cs

public class Blog
{
    public int Id { get; set; }
}

--- Context.cs

using Microsoft.EntityFrameworkCore;

public class Context : DbContext
{
    public DbSet<Blog> Blogs { get; set; } = null!;

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        optionsBuilder.UseSqlite("Data Source=tmp.db");
    }
}

--- Program.cs

using var context = new Context();
_ = context.Blogs.FirstOrDefault();

Stack traces


Verbose output


EF Core version

10.0.3, 11.0.0-preview.2.26159.112

Database provider

Microsoft.EntityFrameworkCore.Sqlite

Target framework

.NET 10, .NET 11

Operating system

Ubuntu 24.04.4 LTS

IDE

No response

Activity

  1. added theissue type on Mar 25, 2026
  2. added this to the Backlog milestone on Mar 25, 2026
  3. self-assigned this
    on Mar 25, 2026
  4. roji commented on Mar 25, 2026

    @roji
    Member

    Thanks for raising this. I'm putting on the backlog as we're looking at better ways to support NativeAOT, but at some point we'll do a push and I'll take a look at fixing this.

  5. nurullahturkoglu commented on Oct 10, 2026

    @nurullahturkoglu

    I'd like to fix this if it's open for a community PR.

    The generated file starts with // <auto-generated />, so the compiler disables the nullable context for it, but the interceptor signatures keep the nullable annotations from the operator signatures (e.g. TSource? for FirstOrDefault). That gives CS8669 whatever the project's Nullable setting is.

    I tried emitting #nullable enable annotations after the #pragma warning disable EF9100 line. That makes the annotations valid without turning on nullable warnings for the rest of the generated code.

    For a test, I made PrecompiledQueryTestHelpers fail on CS8669 in the generated files. On main that fails 33 of the SQLite precompiled query tests, and with the change they pass. Does that sound OK? If so I'll open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions