原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.aspx

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类:

  1. public class Student
  2. {
  3. public int StudentID { get; set; }
  4.  
  5. public string Name { get; set; }
  6.  
  7. public int Age { get; set; }
  8.  
  9. public string Sex { get; set; }
  10. }

3.创建一个上下文类:

  1. public class EFDbContext:DbContext
  2. {
  3. public EFDbContext() : base("name=Constr")
  4. {
  5. }
  6. public DbSet<Student> Students { get; set; }
  7. }

4.App.config配置文件:【数据库连接字符串】

  1. <!--数据库连接字符串-->
  2. <connectionStrings>
  3. <add name="Constr" connectionString="Server=.;Database=EFAnnotationKeyDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient"/>
  4. </connectionStrings>

5.测试代码:

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. using (var db = new EFDbContext())
  6. {
  7. List<Student> lstStuModel = db.Students.ToList();
  8. }
  9.  
  10. Console.WriteLine("success");
  11. Console.ReadKey();
  12. }
  13. }

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 系列】的更多相关文章

  1. 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...

  2. 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...

  3. 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...

  4. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...

  5. 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...

  6. 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】

    原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...

  7. 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code ...

  8. 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx EF ...

  9. 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-f ...

随机推荐

  1. spring 之 BeanPostProcessor

    粗略一看, 它有这么多实现: 可见, 它是多么基础 而重要的一个 接口啊! 它提供了两个方法: public interface BeanPostProcessor { Object postProc ...

  2. python——字符串问题总结

    转义符r/R使用: print (r'\n') print (R'\n') 输出: \n \n 不受转义符\n影响 python字符串格式化: print ("我叫 %s 今年 %d 岁!& ...

  3. shell数组的使用

    定义:  array=(1 2 3) echo ${array[0]} echo ${array[1]} echo ${array[2]} echo ${array[*]}   所有元素 echo $ ...

  4. Sybase采用load table加载文本数据中的部分字段数据

    LOAD TABLE语句实现从数据文件中装载部分列数据的方法.转载自:https://www.cnblogs.com/lizm166/p/8116475.html(有修改) LOAD TABLE:从数 ...

  5. Pandas基础知识(二)

    Pandas的索引对象 index的对象是不可以修改的如执行index[1] = 'f',会报错"Index does not support mutable operations" ...

  6. java学习笔记(十):scanner输入

    可以通过 Scanner 类来获取用户的输入. 通过next()类和nextLine()类来获取字符串. 通过 Scanner 类的 next() 类来获取输入的字符串. import java.ut ...

  7. Django 的ORM 数据操作

    from teatcher.models import Student      导入app(teatcher)下的模型(Student) In [11]: res = Student.objects ...

  8. FortiGate下视频会议等语音相关配置

    关闭老的基于会话的alg机制(即删除session-helper中的SIP条目) config system session-helper delete 13  #删除sip end

  9. 22. Generate Parentheses产生所有匹配括号的方案

    [抄题]: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  10. js函数内未声明变量

    <script> function test(){ testd = "Hello"; } test(); alert(testd); </script> 当 ...