Compiled model: generate a deterministic model ID

Abierto
#39,855 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

@rgroenewoudt ya está trabajando en esto.

Desde el 29/11/2026.

  • #39856 de @rgroenewoudt — abierto

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
csharp

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C#
Estrellas
14.8k
Forks
3.5k
Merge medio
2 d 22 h
PR fusionados (30 d)
158

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de dotnet/efcore

Todos los issues de dotnet/efcore

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.