Toolbelt.EntityFrameworkCore.IndexAttribute 3.0.0

Revival of [Index] attribute for EF Core. (with extension for model building.)
This package also provides [PrimaryKey] attribute.

[NOTICE] EF Core team said "We didn't bring this over from EF6.x because it had a lot of issues" (https://github.com/aspnet/EntityFrameworkCore/issues/4050)
You should consider well before use this package.

Install-Package Toolbelt.EntityFrameworkCore.IndexAttribute -Version 3.0.0
dotnet add package Toolbelt.EntityFrameworkCore.IndexAttribute --version 3.0.0
<PackageReference Include="Toolbelt.EntityFrameworkCore.IndexAttribute" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Toolbelt.EntityFrameworkCore.IndexAttribute --version 3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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.0 | v.3.0
v.2.0, 2.1, 2.2 | v.2.0.x

If you use SQL Server and "IsCLustered=true" feature, you need to add Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer package, 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" feature, 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.

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" feature.
    modelBuilder.BuildIndexesFromAnnotations(options => {
      options.SuppressNotSupportedException.IsClustered = 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.1.0
    • Add "IsClustered" index property
    • Add "PrimaryKey" attribute class
  • v.1.0.0 - 1st release.

Toolbelt.EntityFrameworkCore.IndexAttribute

  • 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.0.0 - BREAKING CHANGE: supports EntityFramework Core v.3.0
  • v.1.0.0 - 1st release for support "IsClustered" property of [Index] attribue on a SQL Server connection.

License

MIT License

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.0 | v.3.0
v.2.0, 2.1, 2.2 | v.2.0.x

If you use SQL Server and "IsCLustered=true" feature, you need to add Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer package, 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" feature, 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.

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" feature.
    modelBuilder.BuildIndexesFromAnnotations(options => {
      options.SuppressNotSupportedException.IsClustered = 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.1.0
    • Add "IsClustered" index property
    • Add "PrimaryKey" attribute class
  • v.1.0.0 - 1st release.

Toolbelt.EntityFrameworkCore.IndexAttribute

  • 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.0.0 - BREAKING CHANGE: supports EntityFramework Core v.3.0
  • v.1.0.0 - 1st release for support "IsClustered" property of [Index] attribue on a SQL Server connection.

License

MIT License

Release Notes

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.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.0.0 272 10/9/2019
2.1.0 892 9/1/2019
2.0.2.3-beta 53 8/22/2019
2.0.1 987 7/26/2019
2.0.0 17,855 2/21/2019
1.0.2.1 1,944 1/22/2019
1.0.1-beta 133 1/18/2019
1.0.0 38,443 12/28/2017