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

我们已经学习,如果你没有在父类中包含外键属性,那么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的更多相关文章

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

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

  2. 【译】第18节---数据注解-ForeignKey

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. python内置函数zip

    zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以 ...

  2. 设计模式之Observer(观察者)(转)

    Java深入到一定程度,就不可避免的碰到设计模式(design pattern)这一概念,了解设计模式,将使自己对java中的接口或抽象类应用有更深的理解.设计模式在java的中型系统中应用广泛,遵循 ...

  3. hive的find_in_set函数

    集合查找函数: find_in_set语法: find_in_set(string str, string strList) 返回值: int说明: 返回str在strlist第一次出现的位置,str ...

  4. flask 单个页面多个表单(单视图处理、多视图处理)

    单个页面多个表单 除了在单个表单上实现多个提交按钮,有时还需要在单个页面上创建多个表单.比如,在程序的主页上同时添加登录和注册表单.当在同一个页面上添加多个表单时,我们需要解决的问题是在视图函数中判断 ...

  5. CXF框架入门(重点)

    l CXF是一个开源的webservice框架 l CXF支持的协议:SOAP.XML/HTTP等 l CXF可以很好的和spring集成 l CXF可以部署到tomcat.jboss.jetty等服 ...

  6. HDU 2064 汉诺塔III (递推)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2064 约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到 ...

  7. ESXi 嵌套KVM虚拟化 配置

    VMware ESXi5.x默认不支持嵌套虚拟化,需要修改相关配置才能支持   1.ESXi5.1主机开通ssh,修改VMware ESXi配置文件使之嵌套虚拟化. 在配置文件后面加入如下配置:vhv ...

  8. SQL表分区之一

    一般情况下,我们建立数据库表时,表数据都存放在一个文件里. 但是如果是分区表的话,表数据就会按照你指定的规则分放到不同的文件里,把一个大的数据文件拆分为多个小文件,还可以把这些小文件放在不同的磁盘下由 ...

  9. FFMPEG结构体分析:AVFrame(解码后的数据)

    https://blog.csdn.net/jxcr1984/article/details/52766524 本文转自: http://blog.csdn.net/leixiaohua1020/ar ...

  10. Linux下php添加memcache扩展

    很多时候我们都会遇到在已经安装的php中添加其它的扩展. 那我们应该怎么做呢? 这样做.(我们的nginx和php都是已经安装好了的,这里就不做赘述了) 首先,我们需要下载php的memcache扩展 ...