Data

EF Core mapping that survives renaming and obfuscation

DEV Community: #efcore Zero Heartbeat
Data

When an obfuscator renames entity and property identifiers, EF Core conventions rebuild the schema from those names. The article lists every place this happens — not just tables and columns — and shows how to pin each one.

Key points

  • TPH discriminator values default to the CLR type name; pin each subtype with HasValue and a literal string.
  • Owned type columns take the owning navigation as prefix; pin every column with HasColumnName inside OwnsOne.
  • Key, index and foreign-key constraint names derive from members; fix them with HasName, HasDatabaseName and HasConstraintName.
  • Shadow properties have no CLR member and survive renaming, but EF.Property with a string key on a real member throws.
  • Generate migrations from the clean Debug build and never write nameof in a hand-edited migration.
  • Check the protected assembly in a decompiler: entities renamed, OnModelCreating still holding the literal schema names.

Why it matters

Most teams only meet EF Core conventions when an obfuscated build hits an existing database, and the symptom is an empty result set rather than an exception, which makes it expensive to diagnose. The valuable part here is the inventory: discriminator values and owned-type prefixes are model details nobody touches, so they get pinned last. If you ship protected assemblies, pinning all of them is cheap, and the integration test against a pre-populated schema is the real safety net. If you never obfuscate, the discriminator advice still pays off the day a subtype class gets renamed.

Real-world use

  • An app that ships an obfuscated assembly but must keep talking to an existing, unchanged database schema.
  • A service reading historical TPH subtype rows that were written with the original type names as discriminator values.
  • An auditing shim that reflects over a navigation property name at runtime and needs a single member excluded from renaming.

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