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

EF 6 Code-First系列文章目录:

在EF 6和EF Core中,数据注解中的ForeignKey特性,是用来在两个实体间配置外键关系。根据默认的约定,当属性的名称与相关实体的主键属性匹配时,EF将该属性作为外键属性。ForeignKey Signature: [ForeignKey(name string)]
name:相关联的导航属性的名称或者相关联的外键属性名称
看看下面实体间的一对多关系:

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; } //Foreign key for Standard
public int StandardId { get; set; }
public Standard Standard { get; set; }
} public class Standard
{
public int StandardId { get; set; }
public string StandardName { get; set; } public ICollection<Student> Students { get; set; }
}

上面的代码例子中,描述了Student和Standard实体中的一对多关系。为了解释一对多关系,Student类包含了一个StandardId属性还有一个引用类型的属性Standard,并且Standard实体包含了一个集合类型的导航属性Students,Student实体中的StandardId属性匹配上了Standard实体中的主键属性名称StandardId,所以Student实体中的StandardId属性将会自动变成外键属性,并在数据表中生成外键:

ForeignKey特性重写了默认的外键约定,他允许我们在依赖实体中【这里是Student】指定外键属性,这个指定的外键属性名称,不需要匹配主体实体【这里是Standard】中的主键属性名称。
使用ForeignKey数据注解特性,可以有以下三种方式:

  1. [ForeignKey(NavigationPropertyName)]应用在依赖实体的外键标量属性上面,ForeignKey里面的name参数,填写导航属性的名称
  2. [ForeignKey(ForeignKeyPropertyName)]应用在依赖实体的导航属性上面,ForeignKey里面的name参数,填写外键属性的名称
  3. [ForeignKey(ForeignKeyPropertyName)]应用在主体实体的导航属性上面,ForeignKey里面的name参数,填写外键属性的名称

1.[ForeignKey] on the foreign key property in the dependent entity

ForeignKey应用在依赖实体的外键属性上面,相关联的导航属性的名称作为ForeignKey的name参数传入:

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; } [ForeignKey("Standard")]
public int StandardRefId { get; set; }
public Standard Standard { get; set; }
} public class Standard
{
public int StandardId { get; set; }
public string StandardName { get; set; } public ICollection<Student> Students { get; set; }
}

在上面的例子中,ForeignKey特性应用于StandardRefId属性上,并且传入导航属性的名称Standard到name参数上,这样就会在Students表中创建一个外键列StandardRefId,这样就不会生成默认的StandardID列了。

2.[ForeignKey] on the navigation property in the dependent entity

ForeignKey特性可以应用在导航属性上面,然后name参数就指定外键属性列的名称:

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; } public int StandardRefId { get; set; } [ForeignKey("StandardRefId")]
public Standard Standard { get; set; }
} public class Standard
{
public int StandardId { get; set; }
public string StandardName { get; set; } public ICollection<Student> Students { get; set; }
}

在上面的例子中,ForeignKey特性应用在Standard导航属性上面,name参数就是外键属性的名称StandardRefId,这样就会在Students数据表中,生成一个StandardRefId外键列,就不会生成默认的StandardId列了。

3.[ForeignKey] on the navigation property in the principal entity

ForeignKey特性可以应用在主体实体的导航属性上面,name参数就指定外键属性的名称:

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; } public int StandardRefId { get; set; }
public Standard Standard { get; set; }
} public class Standard
{
public int StandardId { get; set; }
public string StandardName { get; set; } [ForeignKey("StandardRefId")]
public ICollection<Student> Students { get; set; }
}

在上面的例子中,ForeignKey特性应用于主体实体Standard中的导航属性Students上,这样同样会在Students表中创建一个外键列StandardRefId.
好了,以上就是数据注解之ForeignKey特性,大家有什么不明白可以留言,感谢支持!

9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】的更多相关文章

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

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

  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. 【算法】后缀自动机(SAM) 例题

    算法介绍见:http://www.cnblogs.com/Sakits/p/8232402.html 广义SAM资料:https://www.cnblogs.com/phile/p/4511571.h ...

  2. 决策树分类算法及python代码实现案例

    决策树分类算法 1.概述 决策树(decision tree)——是一种被广泛使用的分类算法. 相比贝叶斯算法,决策树的优势在于构造过程不需要任何领域知识或参数设置 在实际应用中,对于探测式的知识发现 ...

  3. BZOJ1406 [AHOI2007]密码箱 数论

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1406 题意概括 求所有数x,满足 x<n 且 x2≡1 (mod  n). n<=2 ...

  4. 如何修改 FastAdmin 弹窗大小?

    如何修改 FastAdmin 弹窗大小? 参考代码 1 如下: buttons: [ { name: 'start', , , , extend: 'data-area=\'["350px& ...

  5. i春秋 “百度杯”CTF比赛 十月场 web题 Backdoor

    0x00: 打开题目,题目中告诉我们这题是文件泄露. 0x01: 通过扫描目录,发现可以扫到的有3个文件 index.php flag.php robots.txt 但是浏览flag.php它告诉我们 ...

  6. 关于H5在移动端架构的优化设计总结

    各大互联网公司采取的策略 一.百度移动前端首页 1. 对于首屏的静态文件css/js,在上线前全部编译直出到HTML文件中:整个首页的渲染只需要一次请求: 2.使用缓存:把不变的js/css/html ...

  7. BZOJ.4289.PA2012 Tax(思路 Dijkstra)

    题目链接 \(Description\) 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价 ...

  8. 1207 ACM 汉诺塔II 数学

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1207 中文题目,在原来三个柱子的情况下(汉诺塔一),增加了一个柱子,难度也增加了. 思路: 思考时尽量和汉 ...

  9. 使用time模块,转化时间格式

    import time ''' 时间戳:表示1970年开始计算的偏移量.我们运用type(时间戳)是float类型 结构化时间:9个元素组成的数组 格式化时间字符串 ''' '''获取当前时间戳''' ...

  10. PAT-A1135. Is It A Red-Black Tree (30)

    已知先序序列,判断对应的二叉排序树是否为红黑树.序列中负数表示红色结点,正数表示黑色结点.该序列负数取绝对值后再排序得到的是中序序列.根据红黑树的性质判断它是否符合红黑树的要求.考察了根据先序序列和中 ...