原文链接:https://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx

EF 6 Code-First系列文章目录:

EF 6提供了Index特性,用来在特定的列上面创建索引。

class Student
{
public int Student_ID { get; set; }
public string StudentName { get; set; } [Index]
public int RegistrationNumber { get; set; }
}

默认情况下,索引的名称是IX_{属性的名称},但是你可以修改属性的名称。
同样你还可以通过IsClustered=true来创建聚合索引,或者通过IsUnique=true来创建唯一索引。

[Index( "INDEX_REGNUM", IsClustered=true, IsUnique=true )]
public int RegistrationNumber { get; set; }

理论学习完了,我们练习一下:
1.创建一个控制台应用程序,安装好EF:

2.创建一个Student类:

public class Student
{
public int StudentID { get; set; } public string StudentName { get; set; } /// <summary>
/// 表 'dbo.Students' 中的列 'RowVersion' 的类型不能用作索引中的键列。
/// string类型的属性,不能创建索引
/// </summary>
//[Index]
//public string RowStringVersion { get; set; } [Index]
public decimal RowDecimalVersion { get; set; }
}

3.创建上下文类:

  public class EFDbContext:DbContext
{
public EFDbContext()
: base("name=Constr")
{ } public DbSet<Student> Students { get; set; }
}

4.SQL连接字符串:

  <!--SQL连接字符串-->
<connectionStrings>
<add name="Constr" connectionString="Server=.;Database=EFAnnotationIndexDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient"/>
</connectionStrings>

5.测试代码:

 class Program
{
static void Main(string[] args)
{
using (var db = new EFDbContext())
{
List<Student> lstModel= db.Students.ToList();
}
Console.WriteLine("success");
Console.ReadKey();
}
}

6.运行程序:

看看生成的数据库:

可以看到RowDecimalVersion列生成了一个默认的索引IX_RowDecimalVersion【不唯一,非聚集】
注意:一个表只能有一个聚集索引,然后不能对字符串类型的列,设置索引.

9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】的更多相关文章

  1. 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code ...

  2. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...

  3. 1 翻译系列:什么是Code First(EF 6 Code First 系列)

    原文链接:http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx EF 6 Code-First系列文章目录 ...

  4. 7.2 数据注解属性--TimeStamp特性【Code-First 系列】

    TimeStamp特性可以应用到领域类中,只有一个字节数组的属性上面,这个特性,给列设定的是tiemStamp类型.在并发的检查中,Code-First会自动使用这个TimeStamp类型的字段. 下 ...

  5. 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...

  6. 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...

  7. 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...

  8. 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...

  9. 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...

随机推荐

  1. mvn2gradle

    mvn项目根目录下,运行 gradle init --type pom 备注: 1)确保build.gradle, settings.gradle不存在 2)gradle 3.1测试通过 3)修改bu ...

  2. mydumper下载安装

    下载地址   https://github.com/maxbube/mydumper [root@gg ~]#cd mydumper [root@gg mydumper]# cmake . -bash ...

  3. TotoriseGit安装

    1.前提 前提是有安装过git. 2.下载 3.安装 二:参考的文档 1.不错的文档 https://www.cnblogs.com/xinlj/p/5978730.html http://blog. ...

  4. 095实战 ETL的数据来源,处理,保存

    1.ETL 数据来源:HDFS 处理方式:Mapreduce 数据保存:HBase 2.为什么保存在Hbase中 数据字段格式不唯一/不相同/不固定,采用hbase的动态列的功能非常适合 因为我们的分 ...

  5. Linux系统之常用文件搜索命令

    (一)常用文件搜索命令 (1)which命令 (2)find命令 (3)locate (4)updatedb (5)grep (6)man (7)whatis (一)常用文件搜索命令 (1)which ...

  6. IdentityServer4-用EF配置Client(一)

    一.背景 IdentityServer4的介绍将不再叙述,百度下可以找到,且官网的快速入门例子也有翻译的版本.这里主要从Client应用场景方面介绍对IdentityServer4的应用. 首先简要介 ...

  7. 开启mysql的远程访问权限

    改表法 1.登陆mysql mysql -u root -p 2.修改mysql库的user表,将host项,从localhost改为%.%这里表示的是允许任意host访问,如果只允许某一个ip访问, ...

  8. python 搭建一个简单的 搜索引擎

    我把代码和爬好的数据放在了git上,欢迎大家来参考 https://github.com/linyi0604/linyiSearcher 我是在 manjaro linux下做的, 使用python3 ...

  9. php 允许浏览器跨域访问web服务端的解决方案

    今天和同事探讨了前后端如何真正实现隔离开发的问题,如果前端单独作为服务发布,势必会涉及到无法直接调用后端的接口的问题,因为浏览器是不允许跨域提交请求的. 所谓跨域访问,就是在浏览器窗口,和某个服务端通 ...

  10. 潭州课堂25班:Ph201805201 redis第四课 (课堂笔记)

    redis支持丰富的数据类型, 是个非关系型数据库.以键值对存储,存在内存里, : string. 字符 list.   列表 set. 集合 zset(sorted set). 有序集合 hash ...