Compiled model: generate a deterministic model ID

オープン
#39,855 コメント 0 件 リアクション 0 件 担当者 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時間
マージ済み PR(30日)
158

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dotnet/efcore のほかの issue

dotnet/efcore の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。