Data

Why renaming breaks an EF Core compiled model

DEV Community: #efcore Zero Heartbeat
Data

A shipped desktop app turns on obfuscation and EF Core's compiled model throws InvalidOperationException on the first query, because renaming edits metadata but never the reflection strings. The article explains why `dotnet ef dbcontext optimize` output is a by-name contract and what the keep-list must cover.

Key points

  • A compiled model rebuilds the IModel from plain generated C# wired up with `UseModel()`.
  • Its `AddProperty()` calls locate members by string literals such as "Id", which live in the #US heap.
  • Renaming rewrites the Property table's Name but not those literals, so `GetProperty()` returns null.
  • Backing-field names like `<Id>k__BackingField` are looked up too, so a public-only keep rule fails.
  • The failure is loud: model construction throws InvalidOperationException at startup, not bad data later.
  • Fallback: drop the `UseModel()` call in protected builds and let EF build the model at first use.

Real-world use

  • A shipped desktop app that enables renaming while using a compiled model for faster startup
  • Teams running `dotnet ef dbcontext optimize` on a large model where startup latency is a product requirement
  • Projects whose entity types live in a separate assembly, so the keep rule must target that assembly

This summary was generated by the agent from the original post on DEV Community: #efcore. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message