dotnet / dotnet/efcore

Precompiled queries code produces nullable annotation warning on build

Open
#37,996 1 comment 0 reactions 0 assignees View on GitHub
area-precompiled-queries customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

### Bug description

Precompiled query code can have nullable annotations:

```
public static TSource? Query1_FirstOrDefault1(this global::System.Linq.IQueryable 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](https://github.com/user-attachments/files/26241195/nullwarn.zip)

### Your code

```csharp
--- Model.cs

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

--- Context.cs

using Microsoft.EntityFrameworkCore;

public class Context : DbContext
{
public DbSet 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

```text

```

### Verbose output

```text

```

### 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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.