-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Missing using directive in generated precompiled query source file #38009
Description
Bug description
Model has two entities, Blog in First namespace, and Author in Second namespace. There are two queries, one for Blogs in Program.cs, and another for Authors in Query.cs.
Queries are precompiled, then build fails with error:
/workspaces/ef-play/usingmiss/Generated/Query.EFInterceptors.Context.cs(103,64): error CS0246: The type or namespace name 'Blog' could not be found (are you missing a using directive or an assembly reference?)
It turns out that there is an unsafe accessor for Blog is added to Query.EFInterceptors.Context.cs, which is not used there. No using directive for First namespace (where Blog is) is added, which leads to the error.
If one of the two queries is removed from code, the problem disappears. It looks like generating one precompiling query source file somehow affects another one.
See attached project with minimal example.
Can be reproduced with:
> dotnet ef dbcontext optimize --output-dir Generated --precompile-queries --nativeaot --verbose
> dotnet build
Your code
--- Model.cs
namespace First
{
public class Blog
{
public int Id { get; private set; }
}
}
namespace Second
{
public class Author
{
public int Id { get; private set; }
}
}
--- Context.cs
using Microsoft.EntityFrameworkCore;
using First;
using Second;
internal class Context : DbContext
{
public DbSet<Blog> Blogs { get; set; } = null!;
public DbSet<Author> Authors { 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.First();
Query.Run();
--- Query.cs
public static class Query
{
public static void Run()
{
using var context = new Context();
_ = context.Authors.First();
}
}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