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

ForeignKey属性可以应用于类的属性。对于ForeignKey关系, 默认Code First约定期望外键属性名与主键属性相匹配。

看以下示例:

public class Student
{
public 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 Standard()
{ }
public int StandardId { get; set; }
public string StandardName { get; set; } public ICollection<Student> Students { get; set; } }

正如你在上面的代码中可以看到的,Student类包括外键StandardId,它是Standard类中的主键属性。 所以现在,Code First将在Students类中创建StandardId列,如下所示:

ForeignKey属性覆盖此约定。 你可以具有与依赖类的主键不同的外键属性名称。

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

如上例所示,Student类包括StandardRefId外键属性名称而不是StandardId。 我们使用标准导航属性中的ForeignKey属性指定它,以便Code-First将将StandardRefId视为外键,如下所示:

ForeignKey属性可以应用于key属性来指定它指向哪个导航属性,如下所示:

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

因此,你可以使用ForeignKey属性为外键属性指定不同的名称。

【译】第18节---数据注解-ForeignKey的更多相关文章

  1. 【译】第20节---数据注解-InverseProperty

    原文:http://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-co ...

  2. 【译】第12节---数据注解-ConcurrencyCheck

    原文:http://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-in-c ...

  3. 【译】第19节---数据注解-NotMapped

    原文:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-fir ...

  4. 【译】第17节---数据注解-Column

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

  5. 【译】第16节---数据注解-Table

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

  6. 【译】第15节---数据注解-StringLength

    原文:http://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-code- ...

  7. 【译】第14节---数据注解-MaxLength/MinLength

    原文:http://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribute-i ...

  8. 【译】第13节---数据注解-Required

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

  9. 【译】第11节---数据注解-TimeStamp

    原文:http://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code-fir ...

随机推荐

  1. LoadRunner录制登录机票网址,并回放,加断言

    回放录制登录过程脚本,加断言 在页面登录的过程如下: 1先进入http://127.0.0.1:1080/WebTours/index.htm 2之后获取userSession信息 3在输入信息后点击 ...

  2. limit小结

    1. Limit接受一个或两个数字参数.参数必须是一个整数常量.如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目. 2. 初始记录行的偏移量是 0(而不是 ...

  3. 转:Process类的使用

    转载自:http://www.oschina.net/code/snippet_119226_6188 一.根据进程名获取进程的用户名? 需要添加对 System.Management.dll 的引用 ...

  4. AtCoder Beginner Contest 083 (AB)

    A - Libra 题目链接:https://abc083.contest.atcoder.jp/tasks/abc083_a Time limit : 2sec / Memory limit : 2 ...

  5. BUAA 111 圆有点挤

    题目描述 gg最近想给女友送两个精美的小礼品:两个底面半径分别为R1和R2的圆柱形宝石,并想装在一个盒子里送给女友. 好不容易找到了一个长方体的盒子,其底面为A*B的矩形,他感觉好像宝石装不进去,但又 ...

  6. hdu some problems in Multi-University Training Contest

    hdu 6103 Kirinriki #include<bits/stdc++.h> using namespace std; int n,m,ans; ]; void doit(int ...

  7. mysql5.5被django抛弃,安装mysql5.7记录

    安装: https://www.jb51.net/article/123004.htm 问题解决: https://blog.csdn.net/zztingfeng/article/details/8 ...

  8. Linux 搭建安装Maven环境

    1.前提条件: 1)下载并安装好JDK .在终端输入命令“java -version”,如果出现类似如下信息说明JDK安装成功. $ java -version java version " ...

  9. 初识wxPython

    wxPython是包装C++编写的wxWidgets跨平台的GUI组件 安装wxPython pip install wxpython import wx def load(event): file ...

  10. oci7编译报’olog’等未定义的引用问题

    前一天在测试一个数据导出的时候,发现oci7编译的时候报了一大堆类似"’oparse’未定义的引用问题",这通常是因为找不到实现库的原因,但是oci相关的库又都是存在的,用oci7 ...