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 ...
随机推荐
- hdu3261
题意: 求至少出现k次的最长字符串(可重复) 题解: 后缀数组维护height数组只要找连续k个的最小值 代码: #include <cstdio> #include <algori ...
- 类属性判断 hasattr getattr
- python界面Tkinter编程(tkMessageBox对话框使用)
python界面Tkinter编程(tkMessageBox对话框使用) 转载 https://blog.csdn.net/m_buddy/article/details/80105154 1 ...
- 程序员之路:python3+PyQt5+pycharm桌面GUI开发(转)
程序员之路:python3+PyQt5+pycharm桌面GUI开发 http://blog.sina.com.cn/s/blog_989218ad0102wz1k.html 先看效果: 图 1 没错 ...
- 洛谷 P1507 NASA的食物计划 【二维费用背包】 || 【DFS】
题目链接:https://www.luogu.org/problemnew/show/P1507 题目背景 NASA(美国航空航天局)因为航天飞机的隔热瓦等其他安全技术问题一直大伤脑筋,因此在各方压力 ...
- xadmin2.0(for Django2.0) 基本设置
一.下载xadmin 1.使用安装工具安装: pip install git+git://github.com/sshwsfc/xadmin.git@django2 2.下载源码: git clone ...
- 8,EasyNetQ-多态发布和订阅
您可以订阅一个接口,然后发布该接口的实现. 我们来看一个例子. 我有一个接口IAnimal和两个实现猫和狗: public interface IAnimal { string Name { get; ...
- loj#2012. 「SCOI2016」背单词
题目链接 loj#2012. 「SCOI2016」背单词 题解 题面描述有点不清楚. 考虑贪心 type1的花费一定不会是优的,不考虑, 所以先把后缀填进去,对于反串建trie树, 先填父亲再填儿子, ...
- 洛谷P1432 倒水问题(CODEVS.1226)
To 洛谷.1432 倒水问题 题目背景 In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were co ...
- post请求的header
HTTP Headers 中的 HTTP请求 Accept-Encoding Accept-Encoding: gzip,deflate 大部分的现代浏览器都支持gzip压缩,并会把这一信息报告给服务 ...