• Home
  • About
  • Privacy
  • Contact
  • DCMA
  • Terms
  • Submit / Write For Us
Friday, May 9, 2025
Android Buzz
  • News
  • Phones
  • Apps
  • Games
  • Reviews
  • Tech
    A Guide to Downloading Ninja Spinki Challenges

    A Guide to Downloading Ninja Spinki Challenges on Android

    Bollywood's Top Iconic Indian Destinations

    Bollywood’s Top Iconic Indian Destinations

    Qualcomm announces Snapdragon AR2, S5 and S3 Gen 2 platforms

    Qualcomm announces Snapdragon AR2, S5 and S3 Gen 2 platforms

    Apple to launch ‘MLS Season Pass’ subscription on February 1 • TechCrunch

    Apple to launch ‘MLS Season Pass’ subscription on February 1 • TechCrunch

    5 Ways to Boost Data Security on the Cloud

    5 Ways to Boost Data Security on the Cloud

    Qualcomm announces Snapdragon 8 Gen 2

    Qualcomm announces Snapdragon 8 Gen 2

    As product-led growth expands, Loops digs into the data to track key metrics • TechCrunch

    As product-led growth expands, Loops digs into the data to track key metrics • TechCrunch

    Data breaches are happening in every organization, no matter the size, says VP encryption, Thales

    Data breaches are happening in every organization, no matter the size, says VP encryption, Thales

    Koo launches a bunch of new features to attract more Indian users as Twitter drama intensifies

    Koo launches a bunch of new features to attract more Indian users as Twitter drama intensifies- Technology News, Firstpost

    • Devices
    • Streaming
    • Security
  • DEV
  • How To
    A Guide to Downloading Ninja Spinki Challenges

    A Guide to Downloading Ninja Spinki Challenges on Android

    Bollywood's Top Iconic Indian Destinations

    Bollywood’s Top Iconic Indian Destinations

    Qualcomm announces Snapdragon AR2, S5 and S3 Gen 2 platforms

    Qualcomm announces Snapdragon AR2, S5 and S3 Gen 2 platforms

    Apple to launch ‘MLS Season Pass’ subscription on February 1 • TechCrunch

    Apple to launch ‘MLS Season Pass’ subscription on February 1 • TechCrunch

    5 Ways to Boost Data Security on the Cloud

    5 Ways to Boost Data Security on the Cloud

    Qualcomm announces Snapdragon 8 Gen 2

    Qualcomm announces Snapdragon 8 Gen 2

    As product-led growth expands, Loops digs into the data to track key metrics • TechCrunch

    As product-led growth expands, Loops digs into the data to track key metrics • TechCrunch

    Data breaches are happening in every organization, no matter the size, says VP encryption, Thales

    Data breaches are happening in every organization, no matter the size, says VP encryption, Thales

    Koo launches a bunch of new features to attract more Indian users as Twitter drama intensifies

    Koo launches a bunch of new features to attract more Indian users as Twitter drama intensifies- Technology News, Firstpost

    • Photography
  • Accessories
  • Business
    A Guide to Downloading Ninja Spinki Challenges

    A Guide to Downloading Ninja Spinki Challenges on Android

    Bollywood's Top Iconic Indian Destinations

    Bollywood’s Top Iconic Indian Destinations

    Qualcomm announces Snapdragon AR2, S5 and S3 Gen 2 platforms

    Qualcomm announces Snapdragon AR2, S5 and S3 Gen 2 platforms

    Apple to launch ‘MLS Season Pass’ subscription on February 1 • TechCrunch

    Apple to launch ‘MLS Season Pass’ subscription on February 1 • TechCrunch

    5 Ways to Boost Data Security on the Cloud

    5 Ways to Boost Data Security on the Cloud

    Qualcomm announces Snapdragon 8 Gen 2

    Qualcomm announces Snapdragon 8 Gen 2

    As product-led growth expands, Loops digs into the data to track key metrics • TechCrunch

    As product-led growth expands, Loops digs into the data to track key metrics • TechCrunch

    Data breaches are happening in every organization, no matter the size, says VP encryption, Thales

    Data breaches are happening in every organization, no matter the size, says VP encryption, Thales

    Koo launches a bunch of new features to attract more Indian users as Twitter drama intensifies

    Koo launches a bunch of new features to attract more Indian users as Twitter drama intensifies- Technology News, Firstpost

    • Marketing
No Result
View All Result
Android Buzz
  • News
  • Phones
  • Apps
  • Games
  • Reviews
  • Tech
    A Guide to Downloading Ninja Spinki Challenges

    A Guide to Downloading Ninja Spinki Challenges on Android

    Bollywood's Top Iconic Indian Destinations

    Bollywood’s Top Iconic Indian Destinations

    Qualcomm announces Snapdragon AR2, S5 and S3 Gen 2 platforms

    Qualcomm announces Snapdragon AR2, S5 and S3 Gen 2 platforms

    Apple to launch ‘MLS Season Pass’ subscription on February 1 • TechCrunch

    Apple to launch ‘MLS Season Pass’ subscription on February 1 • TechCrunch

    5 Ways to Boost Data Security on the Cloud

    5 Ways to Boost Data Security on the Cloud

    Qualcomm announces Snapdragon 8 Gen 2

    Qualcomm announces Snapdragon 8 Gen 2

    As product-led growth expands, Loops digs into the data to track key metrics • TechCrunch

    As product-led growth expands, Loops digs into the data to track key metrics • TechCrunch

    Data breaches are happening in every organization, no matter the size, says VP encryption, Thales

    Data breaches are happening in every organization, no matter the size, says VP encryption, Thales

    Koo launches a bunch of new features to attract more Indian users as Twitter drama intensifies

    Koo launches a bunch of new features to attract more Indian users as Twitter drama intensifies- Technology News, Firstpost

    • Devices
    • Streaming
    • Security
  • DEV
  • How To
    A Guide to Downloading Ninja Spinki Challenges

    A Guide to Downloading Ninja Spinki Challenges on Android

    Bollywood's Top Iconic Indian Destinations

    Bollywood’s Top Iconic Indian Destinations

    Qualcomm announces Snapdragon AR2, S5 and S3 Gen 2 platforms

    Qualcomm announces Snapdragon AR2, S5 and S3 Gen 2 platforms

    Apple to launch ‘MLS Season Pass’ subscription on February 1 • TechCrunch

    Apple to launch ‘MLS Season Pass’ subscription on February 1 • TechCrunch

    5 Ways to Boost Data Security on the Cloud

    5 Ways to Boost Data Security on the Cloud

    Qualcomm announces Snapdragon 8 Gen 2

    Qualcomm announces Snapdragon 8 Gen 2

    As product-led growth expands, Loops digs into the data to track key metrics • TechCrunch

    As product-led growth expands, Loops digs into the data to track key metrics • TechCrunch

    Data breaches are happening in every organization, no matter the size, says VP encryption, Thales

    Data breaches are happening in every organization, no matter the size, says VP encryption, Thales

    Koo launches a bunch of new features to attract more Indian users as Twitter drama intensifies

    Koo launches a bunch of new features to attract more Indian users as Twitter drama intensifies- Technology News, Firstpost

    • Photography
  • Accessories
  • Business
    A Guide to Downloading Ninja Spinki Challenges

    A Guide to Downloading Ninja Spinki Challenges on Android

    Bollywood's Top Iconic Indian Destinations

    Bollywood’s Top Iconic Indian Destinations

    Qualcomm announces Snapdragon AR2, S5 and S3 Gen 2 platforms

    Qualcomm announces Snapdragon AR2, S5 and S3 Gen 2 platforms

    Apple to launch ‘MLS Season Pass’ subscription on February 1 • TechCrunch

    Apple to launch ‘MLS Season Pass’ subscription on February 1 • TechCrunch

    5 Ways to Boost Data Security on the Cloud

    5 Ways to Boost Data Security on the Cloud

    Qualcomm announces Snapdragon 8 Gen 2

    Qualcomm announces Snapdragon 8 Gen 2

    As product-led growth expands, Loops digs into the data to track key metrics • TechCrunch

    As product-led growth expands, Loops digs into the data to track key metrics • TechCrunch

    Data breaches are happening in every organization, no matter the size, says VP encryption, Thales

    Data breaches are happening in every organization, no matter the size, says VP encryption, Thales

    Koo launches a bunch of new features to attract more Indian users as Twitter drama intensifies

    Koo launches a bunch of new features to attract more Indian users as Twitter drama intensifies- Technology News, Firstpost

    • Marketing
Android Buzz
No Result
View All Result
Home How To

Entity Framework Core performance tips

by Staff Writer
in How To
network speed
62
SHARES
564
VIEWS
Share on FacebookShare on Twitter

You can improve data access performance in Entity Framework Core in several ways. These include enabling eager loading, disabling lazy loading, using streaming instead of buffering, and disabling change tracking. In this article, we will explore some of the tips and tricks that can help you improve the performance of your ASP.NET Core 7 applications that make use of EF Core 7.

To work with the code examples provided in this article, you should have Visual Studio 2022 Preview installed in your system. If you don’t already have a copy, you can download Visual Studio 2022 Preview here.

Table of Contents

Toggle
  • Create an ASP.NET Core minimal Web API project in Visual Studio 2022 Preview
    • READ ALSO
    • Mastering Screenshots on the Galaxy S8 and S8 Plus: A How To Guide
    • How to Enable Wi-Fi Hotspot on Verizon Galaxy S8/S8+ Without Root Access
    • How to Play Youtube Videos in the Background With the Screen Off
    • How to Download the Android Email APK from Easily Do
  • What is Entity Framework Core?
  • EF Core performance best practices
    • Disable change tracking for read-only scenarios
    • Retrieve only the data you need
    • Split your large data context into many smaller data contexts
    • Disable lazy loading
    • Use DbContext pooling

Create an ASP.NET Core minimal Web API project in Visual Studio 2022 Preview

First off, let’s create an ASP.NET Core project in Visual Studio 2022. Following these steps will create a new ASP.NET Core Web API 7 project in Visual Studio 2022:

READ ALSO

Mastering Screenshots on the Galaxy S8 and S8 Plus A How To Guide

Mastering Screenshots on the Galaxy S8 and S8 Plus: A How To Guide

January 19, 2025
How to Enable Wi-Fi Hotspot on Verizon Galaxy Without Root Access

How to Enable Wi-Fi Hotspot on Verizon Galaxy S8/S8+ Without Root Access

January 17, 2025
How to Play Youtube Videos in the Background With the Screen Off

How to Play Youtube Videos in the Background With the Screen Off

January 18, 2025
How to Download the Android Email APK from Easily Do

How to Download the Android Email APK from Easily Do

January 16, 2025
  1. Launch the Visual Studio 2022 Preview IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “ASP.NET Core Web API” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window, specify the name and location for the new project.
  6. Optionally check the “Place solution and project in the same directory” check box, depending on your preferences.
  7. Click Next.
  8. In the “Additional Information” window shown next, under Framework, select .NET 7.0 (Preview).
  9. Uncheck the check box that says “Use controllers…” since we’ll be using minimal APIs in this example. Leave the “Authentication Type” set to “None” (default).
  10. Ensure that the check boxes “Enable Docker,” “Configure for HTTPS,” and “Enable Open API Support” are unchecked as we won’t be using any of those features here.
  11. Click Create.

We’ll use this ASP.NET Core 7 Web API project to work with Entity Framework Core 7 in the subsequent sections of this article.

What is Entity Framework Core?

Entity Framework is Microsoft’s object-relational mapper (ORM) for .NET. Entity Framework Core is the open-source, cross-platform version of Entity Framework for .NET Core. 

Entity Framework Core makes it easier to implement data access in your .NET Core applications because it allows you to work with the database using .NET objects. EF Core lets you write code to execute CRUD actions (create, read, update, and delete) without understanding how the data is persisted in the underlying database. Using EF Core, you can more easily retrieve entities from the data store, add, change, and delete entities, and traverse entity graphs.

EF Core performance best practices

You can help EF Core perform these data access operations more speedily by taking advantage of a few best practices. We’ll discuss five of these best practices below.

Disable change tracking for read-only scenarios

Whenever you query entities in your DbContext, the context tracks the returned objects so that you can alter them and preserve the changes. If the query is a read-only query, i.e., if no changes will be made to the returned data, then the context is not required to perform that task. You should disable change tracking if it is not required.

You can disable change tracking for individual queries by including the AsNoTracking method in the query. When the AsNoTracking method is used, EF Core will skip the extra effort of tracking the entities, thereby improving performance (especially for queries involving large numbers of entities).

Most importantly, you do not need change tracking when you only intend to retrieve data in your application. In other words, if you only want to retrieve data from the data context, without inserting, updating, or deleting data, then you don’t need this feature to be turned on. You can disable object tracking by adding the following code to your data context class.

ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;

The bottom line is that queries that use AsNoTracking will run faster than queries that don’t use it. However, remember that you must never use AsNoTracking in queries that insert, edit, or delete entities. Furthermore, if you need to insert, edit, or delete data using the data context, you should avoid specifying the QueryTrackingBehavior at the data context level.

Retrieve only the data you need

When dealing with massive volumes of data, you should strive to retrieve only the required records for the specific query. When fetching data, you should use projections to pick just the required fields. You should avoid retrieving unnecessary fields. The following code snippet shows how to obtain data in a paged fashion. Notice how the beginning page index and page size have been used to choose just the required data.

int pageSize = 50, startingPageIndex = 1;
var dataContext = new OrderProcessingDbContext();
var data = dataContext.Orders.Take(pageSize)
.Skip(startingPageIndex * pageSize)
.ToList();

Split your large data context into many smaller data contexts

The data context in your application represents your database. Hence, you may wonder whether the application should have only one or more data contexts. In Entity Framework Core, the startup time of a large data context represents a significant performance constraint. As a result, instead of using a single vast data context, you should break the data context into numerous smaller data contexts.

Ideally, you should only have one data context per module or unit of work. To use multiple data contexts, simply create a new class for each data context and extend it from the DbContext class.

Disable lazy loading

Lazy loading is a feature that eliminates the need to load unnecessary related entities (as in explicit loading) and seems to remove the developer from dealing with related entities entirely. Because EF Core is adept at automatically loading related entities from the database when accessed by your code, lazy loading seems like a nice feature.

However, lazy loading is especially prone to generating unnecessary additional round trips, which could slow down your application. You can turn off lazy loading by specifying the following in your data context:

ChangeTracker.LazyLoadingEnabled = false;

Use DbContext pooling

An application typically has multiple data contexts. Because DbContext objects may be costly to create and dispose of, EF Core offers a mechanism for pooling them. By pooling, DbContext objects are created once, then reused when needed.

Using a DbContext pool in EF Core can improve performance by reducing the overhead involved in building and disposing of DbContext objects. Your application may also use less memory as a result.

The following code snippet illustrates how you can configure DbContext pooling in the Program.cs file.

builder.Services.AddDbContextPool<MyDbContext>(options => options.UseSqlServer(connection));

This article provided a discussion of best practices that can be adopted to improve data access performance in EF Core. Of course, every application has different data access requirements and characteristics. You should benchmark your EF Core performance before and after you apply these changes to assess the results for your specific application. An excellent tool for the task is BenchmarkDotNet, which you can read about in a previous post.

Copyright © 2022 IDG Communications, Inc.

Source by www.infoworld.com

Share25Tweet16SendPin6
Previous Post

Burnout at work is getting worse, according to new data

Next Post

The 6 Best Record Players of 2022 – Review Geek

Related Posts

Mastering Screenshots on the Galaxy S8 and S8 Plus A How To Guide
How To

Mastering Screenshots on the Galaxy S8 and S8 Plus: A How To Guide

January 19, 2025
How to Play Youtube Videos in the Background With the Screen Off
Apps

How to Play Youtube Videos in the Background With the Screen Off

January 18, 2025
How to Enable Wi-Fi Hotspot on Verizon Galaxy Without Root Access
How To

How to Enable Wi-Fi Hotspot on Verizon Galaxy S8/S8+ Without Root Access

January 17, 2025
How to Download the Android Email APK from Easily Do
Apps

How to Download the Android Email APK from Easily Do

January 16, 2025
How to Fluently Remove Junk prints in WhatsApp
How To

How to Fluently Remove Junk prints in WhatsApp

July 10, 2024
How to unleash Galaxy S8 without point scanner
How To

How to unleash Galaxy S8 without point scanner

October 23, 2023
How to use Smart Switch to Update Your Device
How To

How to use Smart Switch to Update Your Device

October 23, 2023
Change Gboard’s Theme
How To

How to Automatically Change Gboard’s Theme to Material Dark Theme at Night

October 21, 2023
Please login to join discussion
  • Home
  • About
  • Privacy
  • Contact
  • DCMA
  • Terms
  • Submit / Write For Us
© 2021 androidbuzz.net
No Result
View All Result
  • News
  • Phones
  • Apps
  • Games
  • Reviews
  • Tech
    • Devices
    • Streaming
    • Security
  • DEV
  • How To
    • Photography
  • Accessories
  • Business
    • Marketing