如何使用 Entity Framework Core 创建聚集索引

编程入门 行业动态 更新时间:2024-10-09 02:21:56
本文介绍了如何使用 Entity Framework Core 创建聚集索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

从 EF6.1 开始,我们有一种在属性上指定聚集索引的方法

From EF6.1, we have a way of specifying a clustered index on a property

public class Person { [Index(IsClustered = true, IsUnique = true)] public long UserName { get; set; } }

但是这个 Index 属性现在似乎不在 EF Core 中?在 EF Core 中,您如何实现这一目标?

But this Index attribute does not seem to be in EF Core right now? In EF Core how do you achieve this?

推荐答案

来自当前的 EF Core 文档 - 索引部分:

From the current EF Core documentation - Indexes section:

数据注释

不能使用数据注释创建索引.

Indexes can not be created using data annotations.

但可以肯定的是,您可以通过 Fluent API 指定它(注意具有 ForSqlServer 前缀的扩展方法似乎表示 SqlServer 特定功能):

But for sure you can specify that via Fluent API (note the extension methods having ForSqlServer prefix which seem to denote SqlServer specific features):

modelBuilder.Entity<Person>() .HasIndex(e => e.UserName) .IsUnique() .ForSqlServerIsClustered();

更新:对于 EF Core 3.0+,该方法仅被称为 IsClustered:

Update: for EF Core 3.0+ the method is called just IsClustered:

modelBuilder.Entity<Person>() .HasIndex(e => e.UserName) .IsUnique() .IsClustered();

更新:从 EF Core 5.0 开始,有 Index 数据注释现在在索引文档链接中提到,但它不能用于指定数据库特定的属性,如集群(特定于 SqlServer),所以原始答案仍然适用.

Update: Starting with EF Core 5.0, there is Index data annotation now mentioned in the Indexes documentation link, but it can't be used to specify database specific attributes like clustered (SqlServer specific), so the original answer still applies.

更多推荐

如何使用 Entity Framework Core 创建聚集索引

本文发布于:2023-10-26 06:37:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529370.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   索引   Framework   Entity   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!