Interacting With Related Data Slides
Interacting With Related Data Slides
Julie Lerman
MOST TRUSTED AUTHORITY ON ENTITY FRAMEWORK
@julielerman thedatafarm.com
Inserting, update & deleting related data
Add
Update
Remove
Attach
EF Core’s Default Entity State of Graph Data
Has Key Value No Key Value
Query is broken
Single Query up into multiple
with Left Join(s) queries sent in a
single command
EF Core 5 (2020)
Filtered Include!
Eager Loading with Include
EF Core 5 (2020)
Filtered Include!
Query Workflow
DbContext connects
the relationships
More Ways to Use Include
_context.Samurais t Get quotes for the samurai
.Include(s => s.Quotes) t Then get the translations for those
quotes
.ThenInclude(q=>q.Translations)
.FirstOrDefault();
_context.Samurais
t Include children & grandchildren
.Include(s=>s.Quotes)
.ThenInclude(q=>q.Translations)
…Various combinations…
Projecting Related Data in Queries
EF Core Can Only Track Entities
Recognized by the DbContext
Explicit Loading
Explicitly retrieve related data for objects already in memory
DbContext.Entry().Collection().Load()
DbContext.Entry().Reference().Load
More on Explicit Loading
OFF
by default
Lazy Loading
DbContext
DbContext
has no clue
is aware
about
of all changes
history of objects
made to objects
before they are
that is it tracking
attached
The Challenge
Edited
Quote
Samurai
Quote Quote
The Challenge
_context.Entry
( Edited
Quote
)
.State
Samurai
Quote Quote
The Challenge
_context.Entry
( Edited
Quote
)
.State
Samurai
Quote Quote
Working with Many-to-Many Relationships
More Likely to Join Existing Objects
Samurai Battle
• Id • BattleId
• Name • Name
• Quotes (List<>) • StartDate
• Horse • EndDate
• Battles (List<>)
* * : • Samurais (List<>)
Simplest convention
No mappings needed
Table is inferred by DbContext
One-to-Many vs. Many-to-Many
• Id • Id • BattleId
• Text • Name • Name
• SamuraiId • Quotes (List<>) • StartDate
• Samurai • Horse • EndDate
• Battles (List<>) • Samurais (List<>)
Explicit
Stored
M2M
Procedure
Mapping
Adding a Many-to-Many Payload to
Existing Join Table & Data
Migrating to explicit
mapping won’t impact
existing data …
Migrating to explicit
mapping won’t impact
existing data …
UNLESS….
Beware of Migration Conventions Conflicts
AppV1 AppV2
Breath easy
Includes a *:* with skip Evolve *:* to include
*:* code still works
navigation payload data
Production Production
Deploy AppV1 Deploy AppV2
Timeline of Events with Fade Transition
AppV1 AppV2
Breath easy
Includes a *:* with skip Evolve *:* to include
*:* code still works
navigation payload data
Production Production
Deploy AppV1 Deploy AppV2
Timeline of Events with Fade Transition
AppV1 AppV2
Breath easy
Includes a *:* with skip Evolve *:* to include
*:* code still works
navigation payload data
Production Production
Deploy AppV1 Deploy AppV2
Timeline of Events with Fade Transition
AppV1 AppV2
Breath easy
Includes a *:* with skip Evolve *:* to include
*:* code still works
navigation payload data
Production Production
Deploy AppV1 Deploy AppV2
Timeline of Events with Fade Transition
AppV1 AppV2
Breath easy
Includes a *:* with skip Evolve *:* to include
*:* code still works
navigation payload data
Production Production
Deploy AppV1 Deploy AppV2
Adding payload mapping to
existing skip navigation
doesn’t break existing code
Explicit Many-to-Many Skip Navigation
Julie Lerman
MOST TRUSTED AUTHORITY ON ENTITY FRAMEWORK
@julielerman thedatafarm.com