9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx
EF 6 Code-First系列文章目录:
- 1 翻译系列:什么是Code First(EF 6 Code First 系列)
- 2.翻译系列:为EF Code-First设置开发环境(EF 6 Code-First系列)
- 3.翻译系列:EF Code-First 示例(EF 6 Code-First系列)
- 4.翻译系列:EF 6 Code-First默认约定(EF 6 Code-First系列)
- 5.翻译系列:EF 6中数据库的初始化(EF 6 Code-First 系列)
- 6.翻译系列:EF 6 Code-First中数据库初始化策略(EF 6 Code-First系列
- 7.翻译系列:EF 6中的继承策略(EF 6 Code-First 系列)
- 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
- 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
- 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
- 9.4 翻译系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
- 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
- 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
- 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
- 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
- 9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】
- 9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】
- 10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
- 10.1.翻译系列:EF 6中的实体映射【EF 6 Code-First系列】
- 10.2.翻译系列:使用Fluent API进行属性映射【EF 6 Code-First】
- 11.翻译系列:在EF 6中配置一对零或者一对一的关系【EF 6 Code-First系列】
- 12.翻译系列:EF 6 中配置一对多的关系【EF 6 Code-First系列】
- 13.翻译系列:Code-First方式配置多对多关系【EF 6 Code-First系列】
- 14.翻译系列:从已经存在的数据库中生成上下文类和实体类【EF 6 Code-First系列】
- 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】
- 16.翻译系列:EF 6 Code -First中使用存储过程【EF 6 Code-First系列】
- 17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】
- 18.翻译系列:EF 6 Code-First 中的Seed Data(种子数据或原始测试数据)【EF 6 Code-First系列】
- 19.翻译系列:EF 6中定义自定义的约定【EF 6 Code-First约定】
- 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】
- 20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】
- 20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】
- 21.翻译系列:Entity Framework 6 Power Tools【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系列】的更多相关文章
- 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code ...
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...
- 1 翻译系列:什么是Code First(EF 6 Code First 系列)
原文链接:http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx EF 6 Code-First系列文章目录 ...
- 7.2 数据注解属性--TimeStamp特性【Code-First 系列】
TimeStamp特性可以应用到领域类中,只有一个字节数组的属性上面,这个特性,给列设定的是tiemStamp类型.在并发的检查中,Code-First会自动使用这个TimeStamp类型的字段. 下 ...
- 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...
- 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...
随机推荐
- mysql 查看正在执行的语句
一.不完全显示: show processlist 二.完全显示: show full processlist
- Python字符串相加以及字符串格式化
1.在Python中字符串a占用一块内存地址,字符串b也占用一块内存地址,当字符串a+b时,又会在内存空间中开辟一块新的地址用来存放a+b. a 地址一 b 地址二 a+b 地址三 因此内存中就占了三 ...
- BZOJ1201 [HNOI2005]数三角形 大力出奇迹
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1201 题意概括 题解 n3跑过去了,大力出奇迹!简单的,不多说了. 代码 #include < ...
- 微信小程序开发--第一个项目
一:Hello World 1.AppId 2.打开开发者工具 3.显示效果 二:
- cglib动态代理导致注解丢失问题及如何修改注解允许被继承
现象 SOAService这个bean先后经过两个BeanPostProcessor,会发现代理之后注解就丢失了. 开启了cglib代理 @SpringBootApplication @EnableA ...
- shell seq 用法
seq [OPTION]... LASTseq [OPTION]... FIRST LASTseq [OPTION]... FIRST INCREMENT LAST seq 1000 ‘起始默认是 ...
- OutputStreramWriter和InputStreamReader类
整个IO类中除了字节流和字符流还包括字节和字符转换流. OutputStreramWriter将输出的字符流转化为字节流 InputStreamReader将输入的字节流转换为字符流 但是不管如何操作 ...
- go协程使用陷阱(转)
协程中使用全局变量.局部变量.指针.map.切片等作为参数时需要注意,此变量的值变化问题. 与for 循环,搭配使用更需谨慎. 1,内置函数时直接使用局部变量,未进行参数传递 package main ...
- android monitor 汉化 ddms
作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com android.jar\com\androi ...
- R1题解
估分 大佬们都去写题解了,我不写可能会被老师训诶.... 预计分数:100 + 100 + 5 + 100 + 25 + 100 = 430 实际 :80 + 100 + 0 + 100 + 25 + ...