Missing using directive in generated precompiled query source file
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
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
```
[usingmiss.zip](https://github.com/user-attachments/files/26274680/usingmiss.zip)
### Your code
```csharp
--- 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 Blogs { get; set; } = null!;
public DbSet 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
```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
Assessment
This issue has not been assessed yet.