Compiled model: generate a deterministic model ID

未关闭
#39,855 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

@rgroenewoudt 已经在做这个了。

开始于 2026年11月29日。

  • #39856 来自 @rgroenewoudt —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
csharp
领域
backend, databases

调研方向

Read the compiled model code generator, especially CSharpRuntimeModelCodeGenerator, and trace how Model.ModelId is emitted. The issue proposes deriving the ID from generated code, but does not name tests or specify how to handle hashing and generation order; first inspect existing compiled model tests and their ModelId baseline setup. Done means unchanged model input generates the same ID and changed model input generates a different ID.

由索引模型根据 Issue 内容生成。

描述

What problem are you trying to solve?

The compiled model code generator writes a random model ID on every run:

private MyContextModel()
    : base(skipDetectChanges: false, modelId: new Guid("11e036cd-643f-f23b-4e7b-adf20935ed15"), entityTypeCount: 473)

Model gets its ID from Guid.NewGuid() and CSharpRuntimeModelCodeGenerator writes model.ModelId as a literal. So running dotnet ef dbcontext optimize on an unchanged model always changes *ModelBuilder.cs.

That is annoying when the compiled model is committed to source control:

  • every regeneration gives a diff, even when nothing changed;
  • a CI check "is the compiled model up to date?" (regenerate and compare) can't be done without masking the ID first;
  • two developers regenerating on separate branches always get a merge conflict on that line.

EF Core's own tests work around it: CompiledModelTestBase sets ModelId = new Guid() to get stable baselines.

Describe the solution you'd like

Derive the model ID from the generated code (e.g. a SHA-256 hash of all generated files) instead of the random ID of the design-time model.

The ID is only used in memory (as a cache key, e.g. in ValueGeneratorCache), so it only has to differ between different models. A hash of the generated code does that, and gives the same output for the same model.

I have a draft PR for this.

主要语言
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 摘要。