All Projects → jsakamoto → Entityframeworkcore.indexattribute

jsakamoto / Entityframeworkcore.indexattribute

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

Programming Languages

csharp
926 projects

Labels

Projects that are alternatives of or similar to Entityframeworkcore.indexattribute

Powershell
PowerShell for every system!
Stars: ✭ 31,244 (+84343.24%)
Mutual labels:  netcore
Tweetinvi
Tweetinvi, an intuitive Twitter C# library for the REST and Stream API. It supports .NET, .NETCore, UAP (Xamarin)...
Stars: ✭ 812 (+2094.59%)
Mutual labels:  netcore
Aws Lambda Dotnet
Libraries, samples and tools to help .NET Core developers develop AWS Lambda functions.
Stars: ✭ 945 (+2454.05%)
Mutual labels:  netcore
Pitstop
This repo contains a sample application based on a Garage Management System for Pitstop - a fictitious garage. The primary goal of this sample is to demonstrate several software-architecture concepts like: Microservices, CQRS, Event Sourcing, Domain Driven Design (DDD), Eventual Consistency.
Stars: ✭ 708 (+1813.51%)
Mutual labels:  netcore
Mudblazor
Blazor Component Library based on Material design. The goal is to do more with Blazor, utilizing CSS and keeping Javascript to a bare minimum.
Stars: ✭ 757 (+1945.95%)
Mutual labels:  netcore
Signalrsample
Real-time Charts with ASP.NET Core SignalR and Chart.js.
Stars: ✭ 23 (-37.84%)
Mutual labels:  netcore
Jose Jwt
Ultimate Javascript Object Signing and Encryption (JOSE) and JSON Web Token (JWT) Implementation for .NET and .NET Core
Stars: ✭ 692 (+1770.27%)
Mutual labels:  netcore
Dispatchproxyasync
System.Reflection.DispatchProxy to Async Proxy
Stars: ✭ 33 (-10.81%)
Mutual labels:  netcore
Micronetes
Micronetes is a local orchestrator inspired by kubernetes that makes developing and testing microservices and distributed applications easier.
Stars: ✭ 785 (+2021.62%)
Mutual labels:  netcore
Avenuenet
AvenueNet 是一个基于 Retrofit 二次封装的网络请求库,使用 Rxjava 的链式调用方式,二次封装的目的是为了对请求数据的正确性以及错误状态做统一的处理,对 Retrofit 对象做管理,有点为业务而生的味道,并非纯正的网络请求库
Stars: ✭ 10 (-72.97%)
Mutual labels:  netcore
Microsoft Authentication Library For Dotnet
Microsoft Authentication Library (MSAL) for .NET
Stars: ✭ 746 (+1916.22%)
Mutual labels:  netcore
Eventsourcing.netcore
Examples and Tutorials of Event Sourcing in .NET Core
Stars: ✭ 760 (+1954.05%)
Mutual labels:  netcore
Mathos Parser
A mathematical expression parser and evaluation library.
Stars: ✭ 26 (-29.73%)
Mutual labels:  netcore
Minet
A (not so) basic Minecraft Pocket Edition server written in C#
Stars: ✭ 706 (+1808.11%)
Mutual labels:  netcore
Restfulsense
A RESTFul operations client that serializes responses and throws meaningful exceptions for >= 400 status codes.
Stars: ✭ 28 (-24.32%)
Mutual labels:  netcore
Epplus
EPPlus 5-Excel spreadsheets for .NET
Stars: ✭ 693 (+1772.97%)
Mutual labels:  netcore
Notlitecode
Remote Encrypted Procedure Calling for .Net & .Net Core
Stars: ✭ 16 (-56.76%)
Mutual labels:  netcore
Lyndacoursesdownloader
Cross platform .net core program to download lynda.com courses for offline use
Stars: ✭ 37 (+0%)
Mutual labels:  netcore
Cookedrabbit
CookedRabbit is a simple service based RabbitMQ wrapper for dealing with channels/connections.
Stars: ✭ 28 (-24.32%)
Mutual labels:  netcore
Imgui.net
An ImGui wrapper for .NET.
Stars: ✭ 848 (+2191.89%)
Mutual labels:  netcore

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

Attention

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.5.0 v.3.1, v.3.2, v.5.0 (Recommended)
v.3.1 v.3.1, v.3.2, v.5.0
v.3.0 v.3.0, v.3.1, v.3.2, v.5.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)
  {
    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.

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

  [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.

Limitations

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

Release Notes

License

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].