Toolbelt.EntityFrameworkCore.IndexAttribute 5.0.1

.NET Standard 2.0
dotnet add package Toolbelt.EntityFrameworkCore.IndexAttribute --version 5.0.1
NuGet\Install-Package Toolbelt.EntityFrameworkCore.IndexAttribute -Version 5.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Toolbelt.EntityFrameworkCore.IndexAttribute" Version="5.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Toolbelt.EntityFrameworkCore.IndexAttribute --version 5.0.1
#r "nuget: Toolbelt.EntityFrameworkCore.IndexAttribute, 5.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Toolbelt.EntityFrameworkCore.IndexAttribute as a Cake Addin
#addin nuget:?package=Toolbelt.EntityFrameworkCore.IndexAttribute&version=5.0.1

// Install Toolbelt.EntityFrameworkCore.IndexAttribute as a Cake Tool
#tool nuget:?package=Toolbelt.EntityFrameworkCore.IndexAttribute&version=5.0.1

IndexColumnAttribute for EntityFramework Core

Build status NuGet Package

What's this?

The [IndexColumn] attribute that is the revival of [Index] attribute for EF Core. (with extension for model building.)


EF Core also includes the [Index] attribute officially, after ver.5.0.

However, I'm going to continue improving and maintaining these libraries, because these libraries still have advantages as below.

  • You can still create indexes by data annotations even if you have to use a lower version of EF Core.
  • You can create indexes with "included columns" for SQL Server.
  • You can create a clustered index (This means you can also create a non-clustered primary key index).

How to use?

  1. Add Toolbelt.EntityFrameworkCore.IndexAttribute package to your project.
> dotnet add package Toolbelt.EntityFrameworkCore.IndexAttribute

Supported Versions

EF Core version This package version
v.6.0 v.5.0.1 (Recommended), v.5.0, v.3.2, v.3.1
v.5.0 v.5.0.1 (Recommended), v.5.0, v.3.2, v.3.1
v.3.1 v.5.0.1 (Recommended), v.5.0, v.3.2, v.3.1
v.3.0 v.5.0.1 (Recommended), v.5.0, v.3.2, v.3.1, v.3.0
v.2.0, 2.1, 2.2 v.2.0.x

If you want to use IsClustered=true and/or Includes index features on a SQL Server, you have to add Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer package to your project, instead.

> dotnet add package Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer
  1. Annotate your model with [IndexColumn] attribute that lives in Toolbelt.ComponentModel.DataAnnotations.Schema.V5 namespace.

Notice - The attribute name is [IndexColumn], is not [Index] (the attribute name [Index] is used by EFocre v.5.0).

using Toolbelt.ComponentModel.DataAnnotations.Schema.V5;

public class Person
    public int Id { get; set; }

    [IndexColumn] // <- Here!
    public string Name { get; set; }
  1. [Important] Override OnModelCreating() method of your DbContext class, and call BuildIndexesFromAnnotations() extension method which lives in Toolbelt.ComponentModel.DataAnnotations namespace.
using Microsoft.EntityFrameworkCore;
using Toolbelt.ComponentModel.DataAnnotations;

public class MyDbContext : DbContext
  // Override "OnModelCreating", ...
  protected override void OnModelCreatin(ModelBuilder modelBuilder)

    // .. and invoke"BuildIndexesFromAnnotations"!

If you use SQL Server and IsClustered=true and/or Includes = new[]{"Foo", "Bar"} features, you need to call BuildIndexesFromAnnotationsForSqlServer() extension method instead of BuildIndexesFromAnnotations() extension method.

  // Override "OnModelCreating", ...
  protected override void OnModelCreatingModelBuilder modelBuilder)

    // Invoke uildIndexesFromAnnotationsForSqlServer"
    // instead of "BuildIndexesFromAnnotations".

That's all!

BuildIndexesFromAnnotations() (or, BuildIndexesFromAnnotationsForSqlServer()) extension method scans the DbContext with .NET Reflection technology, and detects [IndexColumn] attributes, then build models related to indexing.

After doing that, the database which created by EF Core, contains indexes that are specified by [IndexColumn] attributes.

Appendix A - Suppress "NotSupportedException"

You will run into "NotSupportedException" when you call BuildIndexesFromAnnotations() with the model which is annotated with the [IndexColumn] attribute that's "IsClustered" property is true, or "Includes" property is not empty.

If you have to call BuildIndexesFromAnnotations() in this case (for example, share the model for different Database products), you can suppress this behavior with configuration, like this.

  protected override void OnModelCreating(ModelBuilder modelBuilder)

    // Suppress "NotSupportedException" for "IsClustered" and "Includes" feature.
    modelBuilder.BuildIndexesFromAnnotations(options => {
      options.SuppressNotSupportedException.IsClustered = true;
      options.SuppressNotSupportedException.Includes = true;

Appendix B - Notice for using "IsClustered=true"

If you annotate the model with "IsClustered=true" index simply like this,

public class Employee {
  public int Id { get; set; }

  [IndexColumn(IsClustered = true)]
  public string EmployeeCode { get; set; }

You will run into 'System.Data.SqlClient.SqlException' like this.

System.Data.SqlClient.SqlException :
Cannot create more than one clustered index on table '(table name)'.
Drop the existing clustered index '(index name)' before creating another.

In this case, you need to annotate a primary key property with [PrimaryKey(IsClustered = false)] attribute explicitly for suppress auto generated primary key to be clustered index.

public class Employee {
  [PrimaryKey(IsClustered = false)] // <- Add this line!
  public int Id { get; set; }

  [IndexColumn(IsClustered = true)]
  public string EmployeeCode { get; set; }

Appendix C - If you want to use only "IndexAttribute" without any dependencies...

If you want to use only "IndexColumnAttribute" class without any dependencies, you can use Toolbelt.EntityFrameworkCore.IndexAttribute.Attribute NuGet package.

Appendix D - Upgrade an existing project

For more detail on this topic, please visit this link.

Appendix E - If you run into a compile error CS0104...

For more detail on this topic, please visit this link.

For More Detail...

This library is designed to have the same syntax as EF 6.x [Index] annotation.

Please visit document site of EF 6.x and [Index] attribute for EF 6.x.


[IndexColumn] attribute with IsClustered=true can apply only not owned entity types.

Release Notes


MIT License

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (11)

Showing the top 5 NuGet packages that depend on Toolbelt.EntityFrameworkCore.IndexAttribute:

Package Downloads

Package Description


Package Description


The `[IndexColumn]` attribute that is the revival of `[Index]` attribute for EF Core. (with extension for model building.) This package also provides [PrimaryKey] attribute.


Bases microservices da Zicard API


Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.1 143,951 11/11/2021
5.0.1-preview.1.0 316 6/30/2021
5.0.0 329,777 8/14/2020
3.2.0 168,266 3/18/2020
3.1.0 98,800 2/5/2020
3.0.0 50,829 10/9/2019
2.1.0 47,367 9/1/2019 385 8/22/2019
2.0.1 3,769 7/26/2019
2.0.0 41,877 2/21/2019 9,356 1/22/2019
1.0.1-beta 476 1/18/2019
1.0.0 63,992 12/28/2017

- Supports EntityFramework Core 6.0