Some developers complain that the Entity Framework is horribly slow. But what they don’t tell you is that they use EF Core to return:
If you learn how to use EF Core properly, it’s a powerful tool for querying the database. Here are 7 tips for more efficient EF Core queries. 1) Projection Projection means you only select the specific columns you need. Instead of retrieving the entire entities. This dramatically improves performance. 2) Split Query .AsSplitQuery() breaks down a query involving a large number of related entities into multiple SQL queries, reducing the size of the result set. 3) Use Async methods If you want to have a more responsive application, use the asynchronous methods that EF Core provides. They free up threads to handle other requests while waiting for I/O operations to complete. AsNoTracking() tells EF Core not to track the entities retrieved. It saves memory and improves performance for read-only operations. 5) Use Count(), Any() Using methods like Count() and Any() allows EF Core to generate more efficient SQL queries. They are more efficient than retrieving and counting entities in memory. 6) Call SaveChangesAsync() once By calling SaveChangesAsync() once after all updates, you reduce the number of trips to the database. This process dramatically improves performance in bulk operations. 7) Reuse query with IQueryable This means you create an initial IQueryable query. Then, use it in different ways to avoid multiple query variables. If your app loads slowly, don’t add a loading spinner on the frontend. And call it a day. Fix the queries in the backend instead. The above 7 tips should help. Enjoy your weekend.
|
Weekly newsletter packed with code-improving tips, tools, and strategies to become a better .NET developer.
Lately, I’ve been paying more attention to what’s happening in the AI space. Maybe because of all the hype that surrounds it. Maybe because of the anxiety of whether AI will take my software development job. That's why I’ve decided to spend some spare time during the Xmas holiday to explore the state of AI software development tools. This email combines: My 1+ years of experience using a paid version of GitHub Copilot. 5+ hours of YouTube videos I’ve consumed in the last 2 weeks. So, let’s...
Today's issue is brought to you by the C# 13 and .NET 9 – Modern Cross-Platform Development Fundamentals. Build confidence in creating professional and high-performance web applications using the latest technologies in C# 13 and .NET 9 by Mark Price. Find out more here: C# 13 and .NET 9 Yesterday, We had a company Xmas party. Before dinner at a restaurant, we went to the escape room event. If you are unfamiliar with escape rooms, they're interactive puzzle experiences where you and your...
2 weeks ago, .NET 9 was released. If you haven’t had time to read the official release docs, don’t worry. I spent 1 hour investigating what's new in .NET 9. So you don't have to. Here are the top 10 improvements for C#, ASP.NET Core, and EF Core. 1. LINQ Index LINQ has always been an extremely useful tool for .NET developers. However, with .NET 9, LINQ comes with 3 new methods. Let's begin with the LINQ Index. The Index method places every collection element against its position within that...