Tutorials Clean Architecture & DDD Mastery

Handling Persistence Ignorance with EF Core

On this page

Persistence Ignorance

Persistence Ignorance is the principle that your domain objects should not know how they are saved to a database.

1. The EF Core Conflict

EF Core usually wants public setters and parameterless constructors. DDD wants private setters and rich constructors. To solve this, we use **Entity Configurations** in the Infrastructure layer. We use .HasField() and .UsePropertyAccessMode() to tell EF Core to use our private backing fields instead of our public properties.

2. Shadow Properties

Sometimes you need data for the database (like a Version column for concurrency) that the domain doesn't care about. You can define these as **Shadow Properties** in EF Core. They exist in the database and the DbContext, but they never appear in your C# Domain Entity. This keeps your domain pure.

3. Architect Insight

Q: "Should I use Data Annotations like [Key] in my Domain?"

Architect Answer: "NO. That is a violation of Clean Architecture because your Domain now depends on System.ComponentModel.DataAnnotations, which is a persistence detail. Always use the **Fluent API** in the Infrastructure layer's OnModelCreating to define your database schema. The Domain stays 100% POCO (Plain Old CLR Object)."

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Clean Architecture & DDD Mastery
Course syllabus
1. Architectural Patterns
2. Domain-Driven Design (DDD) Foundations
3. Advanced DDD Patterns
4. Implementing the Clean Layers
5. Patterns for Data & Logic
6. Enterprise Domain Challenges
7. Testing Clean Architecture
8. Real-World Case Study
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details