原文链接: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. 菜单联动,select联动菜单(搜索之后默认选中)

    框架:thinkphp php控制器代码: $schedulelist = M('schedule')->getField('sid,schedule_name'); $this->ass ...

  2. Enrolment API

    由于Moodle 2.0有一个用户注册的新概念,它们完全独立于角色和功能.能力通常与注册状态结合使用. 什么是注册? 登记的用户可以完全参加一门课程.活跃用户注册允许用户输入课程.只有注册的用户可能是 ...

  3. NDK官方下载链接

    注:本文转载于成江海:<Android各个版本的NDK官方下载链接 > NDK官方网站:https://developer.android.google.cn/ndk/downloads/ ...

  4. Java开发人员必须掌握的Linux命令(二)

    子曰:"工欲善其事,必先利其器." 学习应该是快乐的,在这个乐园中我努力让自己能用简洁易懂(搞笑有趣)的表达来讲解让知识或者技术,让学习之旅充满乐趣,这就是写博文的初心. 本篇的旅 ...

  5. 安卓工作室 Android studio 或 Intellij IDEA 美化 修改 汉化 酷炫 装逼 Android studio or Intellij IDEA beautify modify Chinesization cool decoration

    安卓工作室 Android studio 或 Intellij IDEA 美化 修改 汉化 酷炫 装逼 Android studio or Intellij IDEA beautify modify ...

  6. c#textBox控件限制只允许输入数字及小数点

    在textboxd的事件中的 KeyPress 事件,这样双击进入代码:输入以下代码 即可 //判断按键是不是要输入的类型. || () && ( && () e.Ha ...

  7. bzoj 3991: [SDOI2015]寻宝游戏 虚树 set

    目录 题目链接 题解 代码 题目链接 bzoj 3991: [SDOI2015]寻宝游戏 题解 发现每次答案就是把虚树上的路径*2 接在同一关键点上的点的dfs序是相邻的 那么用set动态维护dfs序 ...

  8. 洛谷.1782.旅行商的背包(背包DP 单调队列)

    题目链接(卡常背包) 朴素的多重背包是: \(f[i][j] = \max\{ f[i-1][j-k*v[i]]+k*w[i] \}\),复杂度 \(O(nV*\sum num_i)\) 可以发现求\ ...

  9. java后端发送请求

    package com.ty.mapapisystem.util; import java.io.BufferedReader;import java.io.FileInputStream;impor ...

  10. 工作笔记—新浪微博Oauth2.0授权 获取Access Token (java)

    java发送新浪微博,一下博客从注册到发布第一条微博很详细 利用java语言在eclipse下实现在新浪微博开发平台发微博:http://blog.csdn.net/michellehsiao/art ...