Entity Framework Core Code-First - Generate Covering Index with Columns Included

access_time 7 months ago visibility464 comment 0

In SQL Server or some other relational databases, it is a very common requirement to create covering index with columns included in index pages beside the index key columns. With Entity Framework Core, you can also easily generate covering indexes using purely C# code.

Scenario

For Kontext website, backend database storage is Azure SQL Database. Most recently, Azure portal recommends creating two indexes to improve query performances. The recommended indexes details look like the following screenshots:

Both recommendations are covering indexes. 

Create indexes using T-SQL

Azure portal also allows us to directly apply these indexes to the database using T-SQL.

For example, the following statements are generated by Azure:

CREATE NONCLUSTERED INDEX [nci_wi_FlexContents_673086CD9DE40ABE97C36E561E7D87B6] ON [flex].[FlexContents] ([Blocked], [IsDeleted], [IsPublished], [Tag]) INCLUDE ([SiteID]) WITH (ONLINE = ON)


CREATE NONCLUSTERED INDEX [nci_wi_FlexActions_6E4A2EE58FF9DE15144A33EDD89D277E] ON [flex].[FlexActions] ([IsDeleted], [SiteID], [ContentID]) INCLUDE ([DateCreated], [Type]) WITH (ONLINE = ON)

I can directly apply these indexes into database however since I'm using code-first approach, I will use Entity Framework APIs to implement this changes.

Entity Framework Core Code-First

Microsoft official documentation shows how to use code-first to create indexes with included columns. 

Refer to the following page for more details:

Indexes

warning Once we add database specific implementations into the model, your code will only work with those databases. This is one drawback of this approach. However, you can always define macros to build your project differently. Refer to this page for more details: #if (C# reference).

Constraints

As included column is not implemented in all databases, thus this API (IncludeProperties) is not directly included Entity Framework core libraries and you need to included the related database extension packages into your project references.

For my case, I need to include SQL Server package:

<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.5" />

If you use other databases, please change the package reference accordingly.

Implement this change

With the extensions methods added by the database specific package, I can now easily add this packages via the following code:

builder.Entity<FlexContent<TKey>>(entity =>
            {
                // Performance improvement index
                entity.HasIndex(e => new { e.Blocked, e.IsDeleted, e.IsPublished, e.Tag })
                .IncludeProperties(e => e.SiteID);

            });

builder.Entity<FlexAction<TKey>>(entity =>
            {
                // Performance improvement index
                entity.HasIndex(e => new { e.IsDeleted, e.SiteID, e.ContentID })
                .IncludeProperties(e => new { e.DateCreated, e.Type });

            });

Generate migrations using CLI

After the models are updated, I can use entity framework CLI to generate migrations:

dotnet ef migrations add FlexContentActionPerformanceIndex

After this, the website project will have a few migrations classes generated automatically as the following screenshot shows:

Apply changes to database

We can run CLI command to apply the changes to databases in different environment:

 dotnet ef database update

The command line generates the following output:

It executes the two statements to create indexes and then insert a migration record into __EFMigrationsHistory table in the database. 

Summary

As shown in the above code snippets, it is very easy to use Entity Framework Core APIs to create indexes in the data model. With IncludeProperties API implemented some database specific packages, you can also create indexes with columns included.

If you prefer to give it a name for your indexes, you can use HasName API. 

copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn or Twitter.

Want to publish your article on Kontext?

Learn more

More from Kontext

Modern Web Application - Azure Blob Storage for Uploaded Files
visibility 2531
thumb_up 0
access_time 10 months ago

With cloud platforms like Azure, we can totally separate user content storage from web application storage to decouple components from each other and to make the application easy to scale and deploy. This article provides detailed information with code snippets about how to use Azure server-less product Blob Storage and App Service to enable horizontally scalable web application for users to upload files (BLOBs).

visibility 286
thumb_up 0
access_time 2 years ago

I’ve just started an asp.net core 2.2 based implementation for Australia Consumer Data Standards (published by Data 61). Opening Banking initiative will follow these standards. The purpose is to help you to get familiar with these standards, especially the APIs that need to be implemented. The ...

Azure SQL Database Automated Backup Strategy
visibility 689
thumb_up 0
access_time 10 months ago

When designing the architecture of Kontext platform, Azure SQL Database is chosen as the storage for relational data. TDE and other advanced security features are always enabled to protect the database. Backup plans are also employed to ensure I can always restore the database for as point of time ...