using-ef-core-to-query-a-database-slides
using-ef-core-to-query-a-database-slides
Database
Julie Lerman
EF Core Expert and Software Coach
@Julielerman | thedatafarm.com
Module Understand EF Core’s query workflow
Overview Basics of querying using EF Core and LINQ
Filtering, sorting, and aggregating in
queries
Explore the SQL queries that EF Core is
building for you
Improve query performance by
deactivating tracking when not needed
Querying Basics
Query Workflow
Adds tracking
Materializes results Receives
details to
as objects tabular results
DbContext instance
The Simplest Query
_context.Authors.ToList()
A LINQ execution method,
e.g., ToList(),
triggers the query to
execute on the database.
Two Ways to Express LINQ Queries
(from a in context.Authors
context.Authors.ToList(); select a)
.ToList()
(from a in context.Authors
context.Authors
where a.FirstName==“Julie”
.Where(a=>a.FirstName==“Julie”)
select a)
.ToList()
.ToList()
More about LINQ Operators
Querying Data in EF Core
Torben Boeck Jensen
var authors = context.Authors.ToList();
_context.Authors.Where(a=> _context.Authors.Where(a=>
EF.Functions.Like(a.Name,“%abc%”) a.Name.Contains(“abc”)
) )
DbSet.Find(keyvalue)
Executes immediately
OrderBy(o=>o.Property) ThenBy(o=>o.Property)
OrderByDescending ThenByDescending
(o=>o.Property) (o=>o.Property)
LINQ Methods Can Be Combined
_context.Authors
.Where(a=>a.LastName=="Lerman")
.OrderByDescending(a=>a.FirstName).ToList();
Aggregating Results in Queries
LINQ to Entities Aggregate Execution Methods
First() FirstAsync()
FirstOrDefault() FirstOrDefaultAsync()
Single() SingleAsync()
SingleOrDefault() SingleOrDefaultAsync()
Last() LastAsync()
LastOrDefault() LastOrDefaultAsync()
Count() CountAsync()
LongCount() LongCountAsync()
Min(), Max() MinAsync(), MaxAsync()
Average(), Sum() AverageAsync(), SumAsync()
No Aggregation
ToList() ToListAsync()
AsEnumerable() AsAsyncEnumerable()
Aggregate Method Pointers
Single methods expect only one match and will throw if there are
none or more than one
Adds tracking
Materializes results Receives
details to
as objects tabular results
DbContext instance
Change tracking is
expensive.
Most Common Scenario for No Tracking:
Web and Mobile Apps
Response
Request Request
(Get) (Put/Post/Delete)
No Track Queries
and DbContexts
var author = t AsNoTracking() returns a query, not a DbSet
context.Authors.AsNoTracking()
.FirstOrDefault();
protected override void OnConfiguring t All queries for this DbContext will
(DbContextOptionsBuilder optionsBuilder) default to no tracking
{
optionsBuilder Use DbSet.AsTracking() for special queries
.UseSqlServer(myconnectionString) that
need to be tracked
.UseQueryTrackingBehavior
QueryTrackingBehavior.NoTracking);
}
EF Core, with provider’s help, transforms
LINQ queries into SQL
Query execution is triggered with specific
Review methods
Workflow includes sending SQL to database
EF Core materializes database results into
objects
LINQ filter, sorting and aggregating
methods are also translated into SQL
You can improve performance by disabling
the default tracking when not needed
Up Next: