【译】第20节---数据注解-InverseProperty
我们已经学习,如果你没有在父类中包含外键属性,那么Code-First会创建{Class Name} _ {Primary Key}外键列。 当您在类之间有多个关系时,会使用InverseProperty属性。
请看以下示例:
public class Student
{
public Student()
{ }
public int StudentID { get; set; }
public string StudentName { get; set; } public Standard CurrentStandard { get; set; }
public Standard PreviousStandard { get; set; }
} public class Standard
{
public Standard()
{ }
public int StandardId { get; set; }
public string StandardName { get; set; } public ICollection<Student> CurrentStudents { get; set; }
public ICollection<Student> PreviousStudents { get; set; } }
如上例所示,Student类包含两个导航属性到Standard类。 Standard类包括Student的两个集合导航属性。 Code-First为此关系创建四列,如下所示:
InverseProperty覆盖此约定并指定属性对齐。 以下示例使用Standard类中的InverseProperty来解决此问题:
public class Student
{
public Student()
{ }
public int StudentID { get; set; }
public string StudentName { get; set; } public Standard CurrentStandard { get; set; }
public Standard PreviousStandard { get; set; }
} public class Standard
{
public Standard()
{ }
public int StandardId { get; set; }
public string StandardName { get; set; } [InverseProperty("CurrentStandard")]
public ICollection<Student> CurrentStudents { get; set; } [InverseProperty("PreviousStandard")]
public ICollection<Student> PreviousStudents { get; set; } }
如上例所示,我们已经将InverseProperty属性应用于CurrentStudents和PreviousStudents属性,并指定了它所属的Student类的参考属性。
这样,Code-First将在Student表中只创建两个外键列,如下所示:
你还可以使用Foreign Key属性来包含具有不同名称的外键属性,如下所示:
public class Student
{
public Student()
{ }
public int StudentID { get; set; }
public string StudentName { get; set; } public int CurrentStandardId { get; set; }
public int PreviousStandardId { get; set; } [ForeignKey("CurrentStandardId")]
public Standard CurrentStandard { get; set; } [ForeignKey("PreviousStandardId")]
public Standard PreviousStandard { get; set; }
} public class Standard
{
public Standard()
{ }
public int StandardId { get; set; }
public string StandardName { get; set; } [InverseProperty("CurrentStandard")]
public ICollection<Student> CurrentStudents { get; set; } [InverseProperty("PreviousStandard")]
public ICollection<Student> PreviousStudents { get; set; } }
以上示例将创建以下列:
因此,你可以在相同类多个关系之间使用InverseProperty 和ForeignKey 属性。
【译】第20节---数据注解-InverseProperty的更多相关文章
- 【译】第19节---数据注解-NotMapped
原文:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-fir ...
- 【译】第18节---数据注解-ForeignKey
原文:http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-fi ...
- 【译】第17节---数据注解-Column
原文:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-first. ...
- 【译】第16节---数据注解-Table
原文:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first.a ...
- 【译】第15节---数据注解-StringLength
原文:http://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-code- ...
- 【译】第14节---数据注解-MaxLength/MinLength
原文:http://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribute-i ...
- 【译】第13节---数据注解-Required
原文:http://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-firs ...
- 【译】第12节---数据注解-ConcurrencyCheck
原文:http://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-in-c ...
- 【译】第11节---数据注解-TimeStamp
原文:http://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code-fir ...
随机推荐
- Win10,Office2013出现“您的组织策略阻止我们为您完成此操作”怎么解决?
"Windows Registry Editor Version 5.00"这是Windows注册表编辑器5.00版的意思新建一个记事本文件,将以下代码直接复制到新建的文本文件中: ...
- flask 自定义验证器(行内验证器、全局验证器)
自定义验证器 在WTForms中,验证器是指在定义字段时传入validators参数列表的可调用对象,下面来看下编写自定义验证器. 行内验证器 除了使用WTForms提供的验证器来验证表单字段,我们还 ...
- mysql数据库介绍
一.数据库概述 二.MySql安装和基本管理 三.mysql基本语句 四.库的操作 五.表的操作 六.数据类型 七.数据类型二 八.完整性约束 九.外键的变种 三种关系 十.数据的增删改 十一.多表查 ...
- mysql03
查询的列不在同一表中必须使用连接内连接,外链接 -- 输出学生姓名以及对应的年级名称 内连接 select studentName,gradeName from student inner join ...
- 每日linux命令学习-sed
Linux的文本处理实用工具主要由sed和awk命令,二者虽然略有差异,但都使用正则表达式,默认使用标准I/O,并且使用管道命令可以将前一个命令的输出作为下一个命令的输入.笔者将在本节学习sed命令. ...
- VMware14安装centos7
win10专业版 虚拟机:14 Pro 1. 新建虚拟机选择典型安装 2. 稍后安装操作系统 3. 选择Linux,版本选择centso7 64位(根据系统选择) 4. 设置虚拟机名称并选择安装位置 ...
- P2221 [HAOI2012]高速公路(线段树)
P2221 [HAOI2012]高速公路 显然答案为 $\dfrac{\sum_{i=l}^r\sum_{j=l}^{r}dis[i][j]}{C_{r-l+1}^2}$ 下面倒是挺好算,组合数瞎搞 ...
- 【题解】Luogu UVA12345 Dynamic len(set(a[L:R]))
原题传送门 这题要用动态莫队,我博客里有介绍 这道题和luogu P1903 [国家集训队]数颜色 / 维护队列差不多,解法就在上面那篇博客里qaq 主要的问题是如何排序? 排序有三个关键字: 1.左 ...
- BeautifulSoup 模块详解
BeautifulSoup 模块详解 BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后遍可以使用他提供的方法进行快速查找指定元素,从而使得在HT ...
- 值类型之间的相互转化,运算符,if条件判断,循环,函数
值类型之间的相互转化 number | string | boolean 一.转换为boolean=>Boolean(a); var num = 10; var s = '123'; var b ...