原文链接: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. mysql 查看正在执行的语句

    一.不完全显示: show processlist 二.完全显示: show full processlist

  2. Python字符串相加以及字符串格式化

    1.在Python中字符串a占用一块内存地址,字符串b也占用一块内存地址,当字符串a+b时,又会在内存空间中开辟一块新的地址用来存放a+b. a 地址一 b 地址二 a+b 地址三 因此内存中就占了三 ...

  3. BZOJ1201 [HNOI2005]数三角形 大力出奇迹

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1201 题意概括 题解 n3跑过去了,大力出奇迹!简单的,不多说了. 代码 #include < ...

  4. 微信小程序开发--第一个项目

    一:Hello World 1.AppId 2.打开开发者工具 3.显示效果 二:

  5. cglib动态代理导致注解丢失问题及如何修改注解允许被继承

    现象 SOAService这个bean先后经过两个BeanPostProcessor,会发现代理之后注解就丢失了. 开启了cglib代理 @SpringBootApplication @EnableA ...

  6. shell seq 用法

    seq [OPTION]... LASTseq [OPTION]... FIRST LASTseq [OPTION]... FIRST INCREMENT LAST seq 1000   ‘起始默认是 ...

  7. OutputStreramWriter和InputStreamReader类

    整个IO类中除了字节流和字符流还包括字节和字符转换流. OutputStreramWriter将输出的字符流转化为字节流 InputStreamReader将输入的字节流转换为字符流 但是不管如何操作 ...

  8. go协程使用陷阱(转)

    协程中使用全局变量.局部变量.指针.map.切片等作为参数时需要注意,此变量的值变化问题. 与for 循环,搭配使用更需谨慎. 1,内置函数时直接使用局部变量,未进行参数传递 package main ...

  9. android monitor 汉化 ddms

    作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com android.jar\com\androi ...

  10. R1题解

    估分 大佬们都去写题解了,我不写可能会被老师训诶.... 预计分数:100 + 100 + 5 + 100 + 25 + 100 = 430 实际 :80 + 100 + 0 + 100 + 25 + ...