Need a little performance boost in your EF Core app? In this video, explore several ways you can optimize your EF Core code.
- Hi, I'm Cam Soper, I'm a content developer with the .NET Docs team here at Microsoft, and welcome back to Entity Framework Core 101. In this video, I'm going to show you some tips to eek the most performance out of your EF Core applications. The first topic we're going to cover is change tracking. When EF Core queries a database, it stores a snapshot of the result set in memory. Any modifications you make to your entities are going to be made against that snapshot, and then later written to the database, In read-only scenarios, where there's no chance you'll actually want to write data back to the database, we can forego that snapshot and conserve system resources. Let's look at how to disable change tracking for a query. To disable change tracking on any result set, all we have to do is add the AsNoTracking method to the query. In our previous videos, we've looked at using navigation properties to load related entities. Entity Framework Core allows you to specify how those related entities are loaded at runtime. We're going to look at two patterns for loading those entities, eager loading and lazy loading. In Visual Studio, we'll look at eager loading first. Eager loading is accomplished by way of the Include method. The Include methods signals to EF Core that the related orders should be loaded on the same database query as the customers. Sometimes, however, it might be advantageous to wait to load the orders until they're actually needed. This is called lazy loading. To enable lazy loading, we're going to get rid of the Include method, then we'll make sure that we have the Microsoft.EntityFrameworkCore.Proxies NuGet package installed. When we add the database context to the dependency injection container, we're going to add the UseLazyLoadingProxies method to the options. And finally, we'll go to our navigation properties and mark them virtual so that Entity Framework Core can override them with the proxies that it generates. Now we've enabled lazy loading and the orders won't be requested from the database until they're actually needed from our code. Sometimes when working with Entity Framework Core you need to use your own SQL, rather than the SQL that it generates for you. Entity Framework Core makes this easy using the FromSqlRaw and FromSqlInterpolated methods. Let's look at using FromSqlInterpolated. I'm going to use FromSqlInterpolated to specify my own SQL query to retrieve my products result set from the database. FromSqlInterpolated uses interpolated strings. Entity Framework Core takes care of converting this interpolated string to a parameterized SQL statement, which prevents against SQL injection attacks. We've previously discussed how Entity Framework Core uses in memory snapshots to track changes to our entities. If we happen to have an entity cached in memory, we can save ourselves a trip to the database by looking it up with the Find or FindAsync methods. In this example, I'm using FirstOrDefaultAsync to look up a record from the database. If I've already got it in my entity snapshot in memory, however, I can retrieve it directly from the snapshot using Find or FindAsync and looking it up by its primary key. If it's not in the entity snapshot, Entity Framework Core will go out to the database to retrieve it. Whenever we use a database context, there's a certain amount of overhead that goes into creating and destroying the object. We can bypass that overhead by using database context pooling to reuse our database context objects over and over again. To use database context pooling, all we have to do is, instead of using AddDbContext, use the AddDbContextPool method. Now our database contexts will be reused over and over again. This concludes our Entity Framework Core 101 series. If you missed any episodes, make sure to go back and watch them. If you need more information about Entity Framework Core, check out our docs or our interactive tutorial at aka.ms/Learn-EF-Core. Thanks for watching.
This course was created by Microsoft.NET. We are pleased to host this training in our library.