如何使用实体框架核心创建聚簇索引

编程入门 行业动态 更新时间:2024-10-19 18:36:42
本文介绍了如何使用实体框架核心创建聚簇索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

如何使用实体框架核心创建聚簇索引

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

发布评论

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

>www.elefans.com

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