9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-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系列】
Key特性应用于实体的一个属性上面,使之成为键属性,然后生成数据库的时候,数据表中相应的列就会成为主键。默认的约束为名称为ID或者实体名称+ID的属性,创建主键。Key特性重写了默认的约定。
正如上面的代码所示,我们在Student实体的StudentKey属性上面,应用于Key特性,这样就会重写默认的约定,就会在Students表的StudentKey列上,创建一个主键:
Key特性可以应用于任何原始数据类型的属性上,除了无符号整形。
EF 6中:
在EF 6中 ,Key特性可以应用于实体的多个属性上面,然后就可以在数据库中,创建联合主键。
EF Core不支持使用数据注解的Key特性,来创建联合主键,在EFCore中你必须使用Fluent API中的HasKey方法,来创建联合主键。
上面的代码,将会在Students表中创建联合主键【StudentKey和AdmissionNum】:
注意的是:Key特性,标识一个列生成的单一主键是自增主键,而复合主键不会生成自增列。
学习不动手,那就是耍流氓,无用功,我们自己动手试试:
1.创建一个名称为EFAnnotationKey的控制台应用程序,安装好EF
2.创建一个Student类:
- public class Student
- {
- public int StudentID { get; set; }
- public string Name { get; set; }
- public int Age { get; set; }
- public string Sex { get; set; }
- }
3.创建一个上下文类:
- public class EFDbContext:DbContext
- {
- public EFDbContext() : base("name=Constr")
- {
- }
- public DbSet<Student> Students { get; set; }
- }
4.App.config配置文件:【数据库连接字符串】
- <!--数据库连接字符串-->
- <connectionStrings>
- <add name="Constr" connectionString="Server=.;Database=EFAnnotationKeyDB;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> lstStuModel = db.Students.ToList();
- }
- Console.WriteLine("success");
- Console.ReadKey();
- }
- }
6.运行代码:
看看生成的数据库:可以看到
1.默认生成的数据表名称为Students,
2.默认的模式名为dbo,
3.默认将StudentID属性映射为主键,并且主键默认是自增的。
4.字符串类型默认生成nvarchar(max),int类型默认生成not null的列
1.我们使用Key 特性来重写一下默认的约定,修改Student实体:
2.修改上下文类:
3.运行程序:
看看生成的数据库:【可以看到,生成一个StudentNumber主键列,并且是自增的】
现在看看复合主键列:【可以看到我们没有使用Column指定两个属性的顺序,先看看可以生成复合主键列不。】
运行程序:报错了,可以看到,我们没有指定两个属性的先后顺序是不行的。
修改一下Student类:这里我们没有按照索引的顺序指定,运行看下:
成功了,我们看下数据库是啥样的:
可以看到:
1.即使没有按照索引属性指定复合主键的顺序,依然能生成数据库以及表,顺序是,优先按照指定的列的顺序,其他没有指定的属性顺序,是按照他们在实体中的先后顺序,在数据库中排列的。
2.生成的复合主键,都不是自增的。
再来看下这样的情况,只对一个属性列,设置顺序,看看:
运行:
可以看到还是不行,必须同时指定列的顺序。
综上所述,我们可以总结如下【EF 数据注解Key特性】:
1.EF默认生成的主键是ID属性列,或者名称为实体名+ID的的属性列,可以通过Key特性重写。
2.生成单一主键列的时候,是自增的,而生成复合主键,都不是自增的
3.EF 生成复合主键,必须同时设置Key特性以及使用Column特性中的Order参数指定所有标志KEY属性的顺序。
9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】的更多相关文章
- 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.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...
- 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】
原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...
- 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code ...
- 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx EF ...
- 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-f ...
随机推荐
- spring 之 BeanPostProcessor
粗略一看, 它有这么多实现: 可见, 它是多么基础 而重要的一个 接口啊! 它提供了两个方法: public interface BeanPostProcessor { Object postProc ...
- python——字符串问题总结
转义符r/R使用: print (r'\n') print (R'\n') 输出: \n \n 不受转义符\n影响 python字符串格式化: print ("我叫 %s 今年 %d 岁!& ...
- shell数组的使用
定义: array=(1 2 3) echo ${array[0]} echo ${array[1]} echo ${array[2]} echo ${array[*]} 所有元素 echo $ ...
- Sybase采用load table加载文本数据中的部分字段数据
LOAD TABLE语句实现从数据文件中装载部分列数据的方法.转载自:https://www.cnblogs.com/lizm166/p/8116475.html(有修改) LOAD TABLE:从数 ...
- Pandas基础知识(二)
Pandas的索引对象 index的对象是不可以修改的如执行index[1] = 'f',会报错"Index does not support mutable operations" ...
- java学习笔记(十):scanner输入
可以通过 Scanner 类来获取用户的输入. 通过next()类和nextLine()类来获取字符串. 通过 Scanner 类的 next() 类来获取输入的字符串. import java.ut ...
- Django 的ORM 数据操作
from teatcher.models import Student 导入app(teatcher)下的模型(Student) In [11]: res = Student.objects ...
- FortiGate下视频会议等语音相关配置
关闭老的基于会话的alg机制(即删除session-helper中的SIP条目) config system session-helper delete 13 #删除sip end
- 22. Generate Parentheses产生所有匹配括号的方案
[抄题]: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- js函数内未声明变量
<script> function test(){ testd = "Hello"; } test(); alert(testd); </script> 当 ...