Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer 3.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer --version 3.2.0                
NuGet\Install-Package Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer -Version 3.2.0                
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.SqlServer" Version="3.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer --version 3.2.0                
#r "nuget: Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer, 3.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer as a Cake Addin
#addin nuget:?package=Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer&version=3.2.0

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

IndexAttribute for EntityFramework Core

Build status NuGet Package

What's this?

Revival of [Index] attribute for EF Core. (with extension for model building.)

Attention

EF Core team said:

"We didn't bring this (= IndexAttribute) over from EF6.x because it had a lot of issues"
(https://github.com/aspnet/EntityFrameworkCore/issues/4050)

Therefore, you should consider well before use this package.

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.3.1, v.5.0 Prev1 v.3.1, v.3.2
v.3.0 v.3.0, v.3.1
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 [Index] attribute that lives in Toolbelt.ComponentModel.DataAnnotations.Schema namespace.
using Toolbelt.ComponentModel.DataAnnotations.Schema;

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

    [Index] // <- 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 OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // .. and invoke "BuildIndexesFromAnnotations"!
        modelBuilder.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 OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // Invoke "BuildIndexesFromAnnotationsForSqlServer"
        // instead of "BuildIndexesFromAnnotations".
        modelBuilder.BuildIndexesFromAnnotationsForSqlServer();
    }

That's all!

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

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

Appendix A - Suppress "NotSupportedException"

You will run into "NotSupportedException" when you call BuildIndexesFromAnnotations() with the model which is annotated with the [Index] 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)
  {
    base.OnModelCreating(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; }

  [Index(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; }

  [Index(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 "IndexAttribute" class without any dependencies, you can use Toolbelt.EntityFrameworkCore.IndexAttribute.Attribute NuGet package.

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.

Limitations

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

Release Note

Toolbelt.EntityFrameworkCore.IndexAttribute.Attibute

  • v.1.2.0 - Add "Includes" index property
  • v.1.1.0
    • Add "IsClustered" index property
    • Add "PrimaryKey" attribute class
  • v.1.0.0 - 1st release.

Toolbelt.EntityFrameworkCore.IndexAttribute

  • v.3.2.0 - Add support for "Includes" property of [Index] attribute
  • v.3.1.0
    • Supports EntityFramework Core v.3.1.0
    • Revert back to .NET Standard 2.0
  • v.3.0.0 - BREAKING CHANGE: supports EntityFramework Core v.3.0
  • v.2.1.0 - Add support [PrimaryKey] attribute.
  • v.2.0.1 - Fix: Doesn't work with owned types on EF Core v.2.1, v.2.2.
  • v.2.0.0 - Splitted [Index] attribute class to another package for detach the dependencies of EF Core from [Index] attribute.
  • v.1.0.2 - Added support for Owned Entity Types.
  • v.1.0.0 - 1st release.

Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer

  • v.3.2.0 - Add support for "Includes" property of [Index] attribute
  • v.3.1.0
    • Supports EntityFramework Core v.3.1.0
    • Revert back to .NET Standard 2.0
  • v.3.0.0 - BREAKING CHANGE: supports EntityFramework Core v.3.0
  • v.1.0.0 - 1st release for support "IsClustered" property of [Index] attribute on a SQL Server connection.

License

MIT License

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

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.1.2 90 11/13/2024
5.0.1.1 958 11/6/2023
5.0.1 31,697 11/11/2021
5.0.1-preview.1.0 202 6/30/2021
5.0.0 28,531 8/14/2020
3.2.1 8,302 7/6/2020
3.2.0 558 3/18/2020
3.1.0 1,273 2/5/2020
3.0.0 592 10/9/2019
1.0.0.2 624 9/1/2019
1.0.0.1-beta 388 8/22/2019

v.3.2.0
- Add support for "Includes" property of [Index] attribute
v.3.1.0
- Supports EntityFramework Core v.3.1.1
- Revert back to .NET Standard 2.0
v.3.0.0
- BREAKING CHANGE: supports EntityFramework Core v.3.0
v.1.0.0
- 1st release.