Interacting With Your EF Core Data Model: Julie Lerman
Interacting With Your EF Core Data Model: Julie Lerman
Julie Lerman
MOST TRUSTED AUTHORITY ON ENTITY FRAMEWORK
@julielerman thedatafarm.com
Exploring SQL generated by EF Core
DbContext
context.Samurais.Add(samuraiObject)
EntityEntry
• Entity
• State
• OriginalValues
• etc.
Under the Covers: Tracking Entities
DbContext
context.Samurais.Add(samuraiObject)
EntityEntry
• Entity
• State
• OriginalValues
• etc.
Under the Covers: Tracking Entities
DbContext
context.Samurais.Add(samuraiObject)
EntityEntry
•
•
•
Entity
State
OriginalValues
SQL
• etc.
Adding Logging to EF Core’s Workload
.NET Core Logging
EF Core
t Delegate to StreamWriter.WriteLine
private StreamWriter _writer
= new StreamWriter
(“EFCoreLog.txt", append: true);
optionsBuilder
.LogTo(_writer.WriteLine)
Formatting
Formatting
context.Samurais.Add(…) context.Add(…)
context.Samurais.AddRange(…) context.AddRange(…)
_context.Samurais.ToList()
Query Workflow
EF Core reads model,
works with provider to
work out SQL
Express & execute query
context.Samurais.ToList() Sends SQL to database
Materializes results
as objects
Adds tracking details Receives tabular results
to DbContext instance
Two Ways to Express LINQ Queries
LINQ Methods LINQ Query Syntax
(from s in context.Samurais
context.Samurais.ToList();
select s).ToList()
var query=_context.Samurais;
var query=_context.Samurais; foreach (var s in query)
{
var samurais=query.ToList();
Console.Writeline(s.name);
}
Deferred Query Execution
var query=_context.Samurais;
Be aware of the
GREAT LINQ OVERHAUL
for EF Core 3, which
impacted query behavior
DbSet.Find(key)
Executes immediately
Like Contains
EF.Functions.Like(property, %abc%) property.Contains(abc)
_context.Samurais.Where(s=> _context.Samurais.Where(s=>
EF.Functions.Like(s.Name,“%abc%”) s.Name.Contains(“abc”)
) )
...Where(s=>s.Name==name)
Find(keyValue) FindAsync(keyValue)
*Last methods require query to have an OrderBy() method otherwise will return full set then pick last in memory
*First/Single will
Execution Method Pointers
Single methods expect only one match and will throw if there are
none or more than one
Context Sends
needs to Set its state relevant SQL
track the to “Deleted” to db on
entity SaveChanges
_context.Samurais.Add(samurai) t DbSet Add, AddRange
_context.Samurais.AddRange(samuraiList)
Fake object with key Stored procedure via Soft delete via
property filled: watch EF Core raw SQL feature Global Query Filters
out for possible side Link in resources
Further on in this course
effects
Understanding Disconnected Scenarios
Working in a Single DbContext Instance
Data
Is DbContext
tracking this
Yes
object?
Mark as
No
modified
Start
tracking
Update causes all
properties to be updated
whether they were
edited or not
Enhancing Performance in Disconnected
Apps with No-Tracking Settings
No Track Queries and DbContext
Logging SQL and Change-Tracking Events in EF Core, MSDN Mag Oct 2019
msdn.microsoft.com/magazine/mt830355
Julie Lerman
MOST TRUSTED AUTHORITY ON ENTITY FRAMEWORK
@julielerman thedatafarm.com