原文链接: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. hdu3261

    题意: 求至少出现k次的最长字符串(可重复) 题解: 后缀数组维护height数组只要找连续k个的最小值 代码: #include <cstdio> #include <algori ...

  2. 类属性判断 hasattr getattr

  3. python界面Tkinter编程(tkMessageBox对话框使用)

    python界面Tkinter编程(tkMessageBox对话框使用)     转载 https://blog.csdn.net/m_buddy/article/details/80105154 1 ...

  4. 程序员之路:python3+PyQt5+pycharm桌面GUI开发(转)

    程序员之路:python3+PyQt5+pycharm桌面GUI开发 http://blog.sina.com.cn/s/blog_989218ad0102wz1k.html 先看效果: 图 1 没错 ...

  5. 洛谷 P1507 NASA的食物计划 【二维费用背包】 || 【DFS】

    题目链接:https://www.luogu.org/problemnew/show/P1507 题目背景 NASA(美国航空航天局)因为航天飞机的隔热瓦等其他安全技术问题一直大伤脑筋,因此在各方压力 ...

  6. xadmin2.0(for Django2.0) 基本设置

    一.下载xadmin 1.使用安装工具安装: pip install git+git://github.com/sshwsfc/xadmin.git@django2 2.下载源码: git clone ...

  7. 8,EasyNetQ-多态发布和订阅

    您可以订阅一个接口,然后发布该接口的实现. 我们来看一个例子. 我有一个接口IAnimal和两个实现猫和狗: public interface IAnimal { string Name { get; ...

  8. loj#2012. 「SCOI2016」背单词

    题目链接 loj#2012. 「SCOI2016」背单词 题解 题面描述有点不清楚. 考虑贪心 type1的花费一定不会是优的,不考虑, 所以先把后缀填进去,对于反串建trie树, 先填父亲再填儿子, ...

  9. 洛谷P1432 倒水问题(CODEVS.1226)

    To 洛谷.1432 倒水问题 题目背景 In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were co ...

  10. post请求的header

    HTTP Headers 中的 HTTP请求 Accept-Encoding Accept-Encoding: gzip,deflate 大部分的现代浏览器都支持gzip压缩,并会把这一信息报告给服务 ...