Compiled model: generate a deterministic model ID

Đang mở
#39,855 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

@rgroenewoudt đang làm issue này rồi.

Từ ngày 29/11/2026.

  • #39856 của @rgroenewoudt — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp
Lĩnh vực
backend, databases

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C#
Star
14.8k
Fork
3.5k
Merge trung bình
2 ngày 22 giờ
Pull request đã merge (30 ngày)
158

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dotnet/efcore

Tất cả issue của dotnet/efcore

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.