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, since 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).
- Add
Toolbelt.EntityFrameworkCore.IndexAttribute
package to your project.
> dotnet add package Toolbelt.EntityFrameworkCore.IndexAttribute
The version of EF Core | Version of this package |
---|---|
v.9.0 | v.5.0.1 or later (Recommended), v.5.0, v.3.2, v.3.1 |
v.8.0 | v.5.0.1 or later (Recommended), v.5.0, v.3.2, v.3.1 |
v.7.0 | v.5.0.1 or later (Recommended), v.5.0, v.3.2, v.3.1 |
v.6.0 | v.5.0.1 or later (Recommended), v.5.0, v.3.2, v.3.1 |
v.5.0 | v.5.0.1 or later (Recommended), v.5.0, v.3.2, v.3.1 |
v.3.1 | v.5.0.1 or later (Recommended), v.5.0, v.3.2, v.3.1 |
v.3.0 | v.5.0.1 or later (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
- Annotate your model with
[IndexColumn]
attribute that lives inToolbelt.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; }
}
- [Important] Override
OnModelCreating()
method of your DbContext class, and callBuildIndexesFromAnnotations()
extension method which lives inToolbelt.ComponentModel.DataAnnotations
namespace.
using Microsoft.EntityFrameworkCore;
using Toolbelt.ComponentModel.DataAnnotations;
public class MyDbContext : DbContext
{
...
// Override "OnModelCreating", ...
protected override void OnModelCreatin(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 OnModelCreatingModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Invoke uildIndexesFromAnnotationsForSqlServer"
// instead of "BuildIndexesFromAnnotations".
modelBuilder.BuildIndexesFromAnnotationsForSqlServer;
}
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.
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)
{
base.OnModelCreating(modelBuilder);
// Suppress "NotSupportedException" for "IsClustered" and "Includes" feature.
modelBuilder.BuildIndexesFromAnnotations(options => {
options.SuppressNotSupportedException.IsClustered = true;
options.SuppressNotSupportedException.Includes = 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; }
}
If you want to use only "IndexColumnAttribute" class without any dependencies, you can use Toolbelt.EntityFrameworkCore.IndexAttribute.Attribute NuGet package.
For more detail on this topic, please visit this link.
For more detail on this topic, please visit this link.
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.