# IndexColumnAttribute for EntityFramework Core [![NuGet Package](https://img.shields.io/nuget/v/Toolbelt.EntityFrameworkCore.IndexAttribute.svg)](https://www.nuget.org/packages/Toolbelt.EntityFrameworkCore.IndexAttribute/) [![unit tests](https://github.com/jsakamoto/EntityFrameworkCore.IndexAttribute/actions/workflows/unit-tests.yml/badge.svg?branch=master&event=push)](https://github.com/jsakamoto/EntityFrameworkCore.IndexAttribute/actions/workflows/unit-tests.yml)
## 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`](https://www.nuget.org/packages/Toolbelt.EntityFrameworkCore.IndexAttribute/) package to your project.
```shell
> dotnet add package Toolbelt.EntityFrameworkCore.IndexAttribute
```
### Supported Versions
The version of EF Core | Version of this package
-----------------------|-------------------------
v.7.0 | **v.5.0.1 (Recommended)**, v.5.0, v.3.2, v.3.1
v.6.0 | **v.5.0.1 (Recommended)**, v.5.0, v.3.2, v.3.1
v.5.0 | **v.5.0.1 (Recommended)**, v.5.0, v.3.2, v.3.1
v.3.1 | **v.5.0.1 (Recommended)**, v.5.0, v.3.2, v.3.1
v.3.0 | **v.5.0.1 (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`](https://www.nuget.org/packages/Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer/) package to your project, instead.
```shell
> dotnet add package Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer
```
2. 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).
```csharp
using Toolbelt.ComponentModel.DataAnnotations.Schema.V5;
public class Person
{
public int Id { get; set; }
[IndexColumn] // <- Here!
public string Name { get; set; }
}
```
3. **[Important]** Override `OnModelCreating()` method of your DbContext class, and call `BuildIndexesFromAnnotations()` extension method which lives in `Toolbelt.ComponentModel.DataAnnotations` namespace.
```csharp
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.
```csharp
...
// 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.
```csharp
...
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,
```csharp
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.
```csharp
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](https://j.mp/3kfJgTm) NuGet package.
## Appendix D - Upgrade an existing project
For more detail on this topic, please visit [this link.](https://j.mp/2HlmNFJ)
## Appendix E - If you run into a compile error CS0104...
For more detail on this topic, please visit [this link.](https://j.mp/3476B3X)
## 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.
- [MSDN Document - Entity Framework Code First Data Annotations](https://j.mp/37hHBZI)
- [MSDN Document - IndexAttribute class](https://j.mp/2HeIAzp)
## Limitations
`[IndexColumn]` attribute with `IsClustered=true` can apply only not owned entity types.
## Release Notes
- [Toolbelt.EntityFrameworkCore.IndexAttribute.Attibute](https://j.mp/3lSWUfw)
- [Toolbelt.EntityFrameworkCore.IndexAttribute](https://j.mp/359Hg90)
- [Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer](https://j.mp/3dBWDuu)
## License
[MIT License](https://j.mp/3476mWB)
没有合适的资源?快使用搜索试试~ 我知道了~
EFCore的Index属性恢复 (带有模型构建的扩展 )-C#-下载
共57个文件
cs:28个
csproj:11个
txt:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 186 浏览量
2023-03-15
19:55:50
上传
评论
收藏 71KB ZIP 举报
温馨提示
(更多详情、使用方法,请下载后细读README.md文件) "EntityFramework Core 的 IndexColumnAttribute 这是什么? 该[IndexColumn]属性是[Index]为 EF Core 复活的属性。(带有模型构建的扩展。) 注意力 EF Core[Index]在 ver.5.0 之后也正式包含该属性。 但是,我将继续改进和维护这些库,因为这些库仍然具有以下优点。 即使您必须使用较低版本的 EF Core,您仍然可以通过数据注释创建索引。 您可以为 SQL Server创建具有“包含的列”的索引。 您可以创建聚簇索引(这意味着您也可以创建非聚簇主键索引)。 如何使用? 将Toolbelt.EntityFrameworkCore.IndexAttribute包添加到您的项目中。 dotnet add package Toolbelt.EntityFrameworkCore.IndexAttribute 支持的版本 EF Core 的版本 这个包的版本 v.7.0 v.5.0.1(推荐)、v.5.0、v.3.2、v.3.1 v.6.0 v.5.0
资源推荐
资源详情
资源评论
收起资源包目录
EFCore的[Index]属性恢复。(带有模型构建的扩展。).zip (57个子文件)
EntityFrameworkCore.IndexAttribute-master
.editorconfig 7KB
EFCore.IndexAttribute.SqlServer
RELEASE-NOTES.txt 558B
AttributedIndexBuilderSqlServerExtension.cs 1KB
EFCore.IndexAttribute.SqlServer.csproj 3KB
README-Appendix-E.md 2KB
EFCore.IndexAttribute
IndexAttribute.cs 271B
RELEASE-NOTES.txt 726B
AttributedIndexBuilderExtension.cs 7KB
AttributedIndexBuilderOptions.cs 957B
EFCore.IndexAttribute.csproj 3KB
AssemblyInfo.cs 131B
.github
workflows
unit-tests.yml 2KB
EFCore.IndexAttribute.UnitTests
EF311
EFCore.IndexAttribute.Test.EF311.csproj 2KB
EF60
EFCore.IndexAttribute.Test.EF60.csproj 2KB
EF70
EFCore.IndexAttribute.Test.EF70.csproj 2KB
MigrationTest
MigrationTest.cs 1KB
TestProject
Program.cs 195B
TestProject.csproj 828B
Models
Person.cs 463B
Data
MyDbContextFactory.cs 607B
MyDbContext.cs 622B
.config
dotnet-tools.json 153B
EFCore.IndexAttribute.Test.MigrationTest.csproj 1KB
EF50
EFCore.IndexAttribute.Test.EF50.csproj 2KB
EF31
EFCore.IndexAttribute.Test.EF31.csproj 2KB
Test
EFCore.IndexAttribute.Test.projitems 1KB
EFCore.IndexAttribute.Test.shproj 1KB
IndexAttributeTest.cs 6KB
Models
Enums
SNSProviders.cs 188B
Address.cs 468B
MyDbContextWithNoConfiguration.cs 578B
SNSAccount.cs 748B
Lines.cs 376B
MyDbContext.cs 696B
MyDbContextBase.cs 2KB
MyDbContextForSqlServer.cs 549B
PhoneNumber.cs 333B
Person.cs 529B
IndexAttributeTest_on_SQLite.cs 3KB
EF30
EFCore.IndexAttribute.Test.EF30.csproj 1KB
.assets
nupkg-icon.png 1KB
nupkg-icon.docx 23KB
LICENSE 1KB
EFCore.IndexAttribute.sln 9KB
dist
.gitkeep 0B
nuget.config 153B
README-Appendix-D.md 893B
EFCore.IndexAttribute.Attribute
PrimaryKeyAttribute.cs 2KB
EFCore.IndexAttribute.Attribute.csproj 2KB
Internals
INameAndOrder.cs 180B
IndexAttribute.cs 3KB
V5
PrimaryKeyAttribute.cs 1KB
IndexColumnAttribute.cs 2KB
RELEASE-NOTES.txt 292B
AssemblyInfo.cs 121B
.gitignore 36B
README.md 7KB
共 57 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9156
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功