dotnet ef dbcontext optimize --precompile-queries does not pass $(Configuration) to MSBuildWorkspace, breaking non-default build configurations

未关闭
#38,951 0 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 2 天内回复

@AndriySvyryd 已经在做这个了。

开始于 2026年9月11日。

评估

这个 Issue 还没有评估数据。

描述

area-aot area-tools
Environment
  • .NET SDK: 11.0.100-rc.1.26425.128
  • Also reproduces on .NET 10 SDKs with EF Core 10.0.12
  • EF Core: 10.0.12 and 11.0.0-rc.1.26425.128
  • OS: macOS arm64
  • This appears to be MSBuild/Roslyn-level behavior and not OS-specific
Reproduction

A minimal project uses a non-default $(Configuration) name with Configuration-conditional TargetFramework and PackageReference values.

For example:

<TargetFramework>net10.0</TargetFramework>

<Configurations>Debug Custom;Release Custom</Configurations>

<PropertyGroup Condition="'$(Configuration)' == 'Debug Custom' Or '$(Configuration)' == 'Release Custom'">
  <TargetFramework>net11.0</TargetFramework>
</PropertyGroup>

EF Core package references are conditioned on the resulting target framework.

Run:

dotnet publish MyProj.csproj --configuration "Release Custom"

The normal build succeeds correctly under Release Custom, producing the expected net11.0 output with the correct EF Core version.

The OptimizeDbContext task then invokes:

dotnet ef dbcontext optimize --precompile-queries --nativeaot

against the already-built application.

That query-precompilation step fails with errors such as:

error CS0234: The type or namespace name 'EntityFrameworkCore' does not exist in the namespace 'Microsoft'

error CS0246: The type or namespace name 'DbContext' could not be found

followed by cascading resolution errors across the project.

Expected behavior

The project reopened by MSBuildWorkspace during query precompilation should be evaluated with the same relevant MSBuild global properties as the build that invoked OptimizeDbContext.

At minimum, the active $(Configuration) should be preserved.

Actual behavior

DbContextOperations.PrecompileQueries creates the workspace with only:

workspace = MSBuildWorkspace.Create(
    new Dictionary<string, string>
    {
        ["_EFGenerationStage"] = "build"
    });

There is also an adjacent:

// TODO: pass through properties

comment.

OpenProjectAsync then re-evaluates the actual .csproj, but without the active Configuration.

For projects whose target framework, package references, project references, or other build properties depend on $(Configuration), that re-evaluation can resolve an entirely different project graph than the build currently in progress.

In the minimal repro, the workspace falls back to the project's unconditional/default target framework and package set rather than the Release Custom graph that was successfully built moments earlier.

The generated-query compilation therefore cannot resolve the application's EF Core references and fails.

Root cause

The issue appears to originate in:

src/EFCore.Design/Design/Internal/DbContextOperations.cs

inside PrecompileQueries, specifically the MSBuildWorkspace.Create(...) call.

The relevant implementation, including the // TODO: pass through properties comment, is unchanged in:

  • EF Core 10.0.12
  • EF Core 11.0.0-rc.1.26425.128
Impact

This blocks Native AOT / precompiled-query publishing for projects using legitimate non-default build configuration names or Configuration-conditioned dependencies.

This is not provider-specific.

I encountered it while enabling EF Core 11 Native AOT support for a third-party provider which intentionally maintains separate builds using:

Debug EF10
Release EF10
Debug EF11
Release EF11

The same problem was then reproduced in a standalone project with no third-party provider code.

Minimal repro

The repro consists of a single project and Program.cs. Two variants were tested:

  • EF Core 11 RC1 with an unconditional net10.0 default overridden to net11.0 by Release Custom
  • EF Core 10.0.12 with the target frameworks reversed

Both reproduce the same failure.

The query-free DbContext is intentional so that this issue remains isolated from unrelated query-translation/precompilation behavior.

I can provide the minimal repro if useful.

Versions

Confirmed affected:

  • Microsoft.EntityFrameworkCore.Design / Tasks 10.0.12
  • Microsoft.EntityFrameworkCore.Design / Tasks 11.0.0-rc.1.26425.128
Possibly related

#36055 hits the same DbContextOperations.PrecompileQueries → MSBuildWorkspace.OpenProjectAsync code path with a different proximate symptom ("The build host could not be found"). Flagging in case the two share a root cause in how MSBuildWorkspace is used here, though the specific defect described above (missing Configuration passthrough) is distinct from that report.

主要语言
C#
星标
14.8k
派生
3.5k
平均合并
2 天 22 小时
30 天内合并 PR
158

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dotnet/efcore 的其他 Issue

查看 dotnet/efcore 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。