Toolbelt.EntityFrameworkCore.IndexAttribute 2.0.0

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

[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 2.0.0
dotnet add package Toolbelt.EntityFrameworkCore.IndexAttribute --version 2.0.0
paket add Toolbelt.EntityFrameworkCore.IndexAttribute --version 2.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
  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();
    }
}

That's all!

BuildIndexesFromAnnotations() 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.

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.

Not Supported Feature

IsClustered property is not supported at this version.

Appendix

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.

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
  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();
    }
}

That's all!

BuildIndexesFromAnnotations() 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.

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.

Not Supported Feature

IsClustered property is not supported at this version.

Appendix

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.

License

MIT License

Release Notes

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.

Version History

Version Downloads Last updated
2.0.0 308 2/21/2019
1.0.2.1 435 1/22/2019
1.0.1-beta 68 1/18/2019
1.0.0 15,843 12/28/2017