How not persist property EF4 code first?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In EF4 Code First, public properties are generally treated as mappable unless excluded by convention or configuration. That becomes a problem when an entity contains computed, UI-only, or temporary fields. The correct approach is to explicitly mark those properties as non-persistent so schema and migrations remain clean and your entity model reflects real persisted state.
Why Non-Persistent Properties Exist
Not every property on an entity belongs in the database.
Common examples:
- Display labels.
- Calculated totals derived from persisted columns.
- Transient workflow flags.
- Values used only for API responses.
If these properties are persisted accidentally, schema grows with noise and future migrations become harder to maintain.
Use NotMapped Attribute
The most direct way is NotMapped.
This keeps those properties available in application code while excluding them from table generation.
One practical consequence is that EF cannot translate those members into SQL just because they exist on the class. If a value is derived from mapped columns, it usually has to be computed after materialization or projected explicitly in the query.
Use Fluent API Ignore for Centralized Mapping
If you prefer domain classes without data-annotation attributes, configure exclusion in Fluent API.
This pattern is useful when multiple teams share entity classes and mapping policy needs centralized ownership.
Separate Entity and View Models
A common source of persistence bugs is mixing UI concerns into entities. For richer response shape, map entity to a view model instead of adding many non-persistent fields to entity class.
This keeps persistence model stable while letting presentation evolve independently.
That separation also makes query intent clearer. Once UI-only fields are out of the entity, it is easier to see which logic belongs in database queries and which logic belongs in application code.
Migration Discipline After Mapping Changes
After adding NotMapped or Ignore:
- Generate migration.
- Review generated operations manually.
- Ensure no unintended column drops appear.
- Run integration tests covering queries and projections.
Do not auto-apply migrations blindly. Mapping changes can affect existing deployments if older columns are still used by downstream reporting jobs.
Edge Cases in Legacy EF4 Projects
In older projects, conventions, proxies, and custom base classes can produce surprising mapping behavior. Make exclusion intent explicit and test schema output directly.
Useful checks:
- Verify table columns in generated database.
- Verify serialization output for API models.
- Verify computed properties still work after lazy-loading or proxy configuration.
Explicit tests around mapping are cheaper than production schema rollback.
Recommended Team Pattern
A practical team standard:
- Default entity classes to persisted domain state only.
- Mark non-persistent properties explicitly with one method, attribute or Fluent.
- Keep mapping review in pull request checklist.
- Validate migration diff in CI.
This reduces accidental schema churn over long-lived projects.
Common Pitfalls
- Assuming getter-only properties are always excluded automatically.
- Mixing UI-specific fields into persistence entities without explicit exclusion.
- Forgetting to review migration output after mapping edits.
- Applying both attribute and Fluent rules inconsistently across modules.
- Removing persistent columns before verifying all consumers are migrated.
Summary
- EF4 Code First will attempt to map many public properties unless excluded.
- Use
NotMappedor FluentIgnoreto prevent persistence explicitly. - Keep computed and presentation fields out of schema whenever possible.
- Separate entity and view models for cleaner architecture.
- Review migration diffs carefully after every mapping change.

