tracking-and-saving-data-with-ef-core-slides
tracking-and-saving-data-with-ef-core-slides
with EF Core
Julie Lerman
EF Core Expert and Software Coach
@julielerman | thedatafarm.com
Module DbContext and entity roles in tracking
Overview Tracking and saving workflow
Inserting, updating, and deleting objects
Tracking and saving multiple objects
Bulk operation support
How persistence differs in
disconnected apps
Gaining a Better Understanding of
DbContext and Entity
A fundamental
understanding of how
things work will pay off in
productivity.
Tracking Components
EF Core creates
DbContext maintains
tracking object for On SaveChanges
EntityEntries
each entity
• Executes statements
on database
context.Authors.Add(…) context.Add(…)
DbContext.ChangeTracker.DetectChanges()
Reads each object being tracked and updates state details in
EntityEntry object
DbContext.SaveChanges()
Always calls DetectChanges for you
If DbContext is
DbSet and
aware of object, it More advanced:
DbContext also
will detect modify state
have an Update()
changes on directly
method
SaveChanges
Updating Untracked Objects
Begin Tracking and Set State to Modified
context.Authors.Update(…) context.Update(…)
Bulk
Delete
Fake object with key Stored procedure Soft delete via ExecuteDelete()
property filled via raw SQL Global Query Filters for untracked data
Watch out for for untracked data Link in resources
possible side Further on in course
effects
Tracking Multiple Objects and Batch
Support
Inserting Multiple Objects
context.Authors.AddRange(…) context.AddRange(…)
context.Authors.UpdateRange(…) context.UpdateRange(…)
context.Authors.RemoveRange(…) context.RemoveRange(…)
/// <summary>
/// The SQL Server limit on parameters, including two extra parameters to
/// sp_executesql (@stmt and @params).
/// </summary>
private const int MaxParameterCount = 2100 - 2;
[Query].ExecuteDelete() [Query].ExecuteUpdate()
Deleting without Querying
Bulk
Delete
Fake object with key Stored procedure Soft delete via ExecuteDelete()
property filled via raw SQL Global Query Filters for untracked data
Watch out for for untracked data Link in resources
possible side Further on in course
effects
Executing Directly in the Database on Untracked Data