interacting-with-related-data-slides
interacting-with-related-data-slides
Data
Julie Lerman
EF Core Expert and Software Coach
@julielerman | thedatafarm.com
Focus on the one-to-many authors & books
Module
Inserting related data
Overview
Eager loading queries and shaping results
with projections
Loading related data for objects in memory
Filtering queries with related data
Update and delete related data
Insights into persisting untracked graphs
Bette Davis in “All About Eve”
Update(graph)
Attach(graph)
EF Core’s Default Entity State of Graph Data
Query Projections
Eager Loading
Define the shape of query
Include related objects in query
results
Receives Materializes
tabular results results as objects
Authors Books for
those Adds tracking
Authors details to
DbContext
instance
DbContext
connects the
relationships
Filtering & Sorting the Included Data
Long requested
By default, the You can filter and
feature that finally
entire collection is sort the related
arrived in
retrieved data
EF Core 5!
Composing Include with Other LINQ Methods
_context.Authors.Where(a=>a.LastName.StartsWith("L"))
.Include(a=>a.Books).ToList()
_context.Authors.Find(1).Include(a=>a.Books)
_context.Authors.Include(a=>a.Books).Find(1)
Remember, Find is not a LINQ method
Using Include for Multiple Layers of Relationships
_context.Authors
.Include(a => a.Books) t Get books for each author
.ThenInclude(b=>b.BookJackets) t Then get the jackets for each book
.ToList();
_context.Authors
t Get books for each author
.Include(a => a.Books)
.Include(a=>a.ContactInfo) t Also get the contact info each author
.ToList();
Query is broken up
Single query
into multiple queries sent
with LEFT JOIN(s)
in a single command
Query Projections
Eager Loading
Define the shape of query
Include related objects in query
results
var someType=_context.Authors
.Select(a=>new {a.FirstName, a.LastName, a.Books.Count() }
.ToList()
someType structure:
FirstName
LastName
Books
Query Projections
Eager Loading
Define the shape of query
Include related objects in query
results
Query Projections
Eager Loading
Define the shape of query
Include related objects in query
results
Explicit Loading
Lazy Loading
Explicitly request related data
On-the-fly retrieval of data
for related to objects in memory
objects in memory
With author object already in memory, load a collection
With book object already in memory, load a reference (e.g., parent or 1:1)
Explicit Loading
Explicitly retrieve related data for objects already in memory
DbContext.Entry(object).Collection().Load()
DbContext.Entry(object).Reference().Load()
More on Explicit Loading
You can only load from a single object Profile to determine if LINQ query
would be better performance
Query Projections
Eager Loading
Define the shape of query
Include related objects in query
results
Behavior to Avoid
var bookCount= author.Books.Count(); t Retrieves all the Book objects from the
database and materialize them and then give you
the count.
t Sends N+1 commands to the database as each
Data bind a grid to lazy-loaded data author’s book is loaded into a grid row
Attach(graph)
Connected Disconnected
Edited
Book
Author
Book Book
The Challenge
Edited
Book
Author
Book Book
The Challenge
_context.Entry
( Edited
Book
)
.State
Author
Book Book
The Challenge
_context.Entry
( Edited
Book
)
.State
Author
Book Book
DbContext.Entry gives you
a lot of fine-grained control
over the change tracker.
Understanding Deleting Within Graphs
Multiple Meanings of Remove/Delete
DELETE thatbook FROM BOOKS t SaveChanges sends DELETE for that book and
DELETE for the author to database
DELETE thatauthor FROM AUTHORS
//database cascade delete any other t The database will delete any remaining books
books in that database for the author
A Few Last Questions about Cascade Delete
Question Answer
Will Remove() remove everything in a No. Remove will only remove the
graph, just like Update? specific object.
What about deleting a dependent that’s You’ve actually done this. If it’s in
not in a graph? memory, DbSet.Remove works. If it’s
not, then ExecuteDelete.
Some More Points About Removing Dependents
What about deleting relationships in This is more advanced, but you will see
disconnected scenarios? some of it in the web app module.
Impact of Optional Relationship