Entity Framework Code-First(9.11):DataAnnotations - InverseProperty Attribute
DataAnnotations - InverseProperty Attribute:
We have seen in the Code-First Convention section that Code-First creates {Class Name}_{Primary Key} foreign key column if you have not included foreign key property in a parent class. The InverseProperty attribute is used when you have multiple relationships between classes.
Consider the following example.
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; } }
As you can see in the above example, Student class includes two navigation properties to Standard class. The same way Standard class includes two collection navigation properties for Student. Code-First creates four columns for this relationship, as shown below.
InverseProperty overrides this convention and specifies alignment of the properties. The following example uses InverseProperty in Standard class to fix this problem.
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; } }
As you can see in the above example, we have applied InverseProperty attribute to CurrentStudents & PreviousStudents property and specify which reference property of Student class it belongs to. Now, Code-First will create only two foreign key column in Student table as shown below.
You can also use ForeignKey attribute to include foreign key property with different name as shown below.
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; } }
The above example will create the following columns.
Thus, you can use InverseProperty and ForeignKey attribute for multiple relationships between the same classes.
Entity Framework Code-First(9.11):DataAnnotations - InverseProperty Attribute的更多相关文章
- Entity Framework Code-First(9.6):DataAnnotations - StringLength Attribute
DataAnnotations - StringLength Attribute: StringLength attribute can be applied to a string type pro ...
- Entity Framework Code-First(9.5):DataAnnotations - MaxLength Attribute
DataAnnotations - MaxLength Attribute: MaxLength attribute can be applied to a string or array type ...
- Entity Framework Code-First(9.10):DataAnnotations - NotMapped Attribute
DataAnnotations - NotMapped Attribute: NotMapped attribute can be applied to properties of a class. ...
- Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute
DataAnnotations - ForeignKey Attribute: ForeignKey attribute can be applied to properties of a class ...
- Entity Framework Code-First(9.8):DataAnnotations - Column Attribute
DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Defaul ...
- Entity Framework Code-First(9.7):DataAnnotations - Table Attribute
DataAnnotations - Table Attribute: Table attribute can be applied to a class. Default Code-First con ...
- Entity Framework Code-First(9.2):DataAnnotations - TimeStamp Attribute
DataAnnotations - TimeStamp Attribute: TimeStamp attribute can be applied to only one byte array pro ...
- Entity Framework Code-First(9.1):DataAnnotations - Key Attribute
DataAnnotations - Key Attribute: Key attribute can be applied to properties of a class. Default Code ...
- Entity Framework Code-First(9.4):DataAnnotations - Required Attribute
Required attribute can be applied to a property of a domain class. EF Code-First will create a NOT N ...
随机推荐
- 20145229吴姗珊《java程序设计》第2次实验报告
20145229吴姗珊<java程序设计>第2次实验报告 实验名称 Java面向程序设计,采用TDD的方式设计有关实现复数类Complex. 理解并掌握面向对象三要素:封装.继承.多态. ...
- Spark- 数据清洗
输入输出转化工具类 package com.rz.mobile_tag.log import org.apache.spark.sql.Row import org.apache.spark.sql. ...
- Spark- Checkpoint原理剖析
Checkpoint,是Spark 提供的一个比较高级的功能.有的时候,比如说,我们的 Spark 应用程序,特别的复杂,然后从初始的RDD开始,到最后拯个应用程序完成,有非常多的步骤,比如超过20个 ...
- Spring MVC工作原理(好用版)
Spring MVC工作原理 参考: SpringMVC工作原理 - 平凡希 - 博客园https://www.cnblogs.com/xiaoxi/p/6164383.html SpringMVC的 ...
- Delphi考虑sql注入 QuotedStr
之前只在BS架构的项目中考虑了Sql注入问题,却很少考虑到用了多年的Delphi项目也应该考虑Sql注入的问题,今天做了个实验,成功完成注入,把表里数据全部删除,以后再做Delphi项目还真的考虑这个 ...
- A N EAR -D UPLICATE D ETECTION A LGORITHM T O F ACILITATE D OCUMENT C LUSTERING——有时间看看里面的相关研究
摘自:http://aircconline.com/ijdkp/V4N6/4614ijdkp04.pdf In the syntactical approach we define binary at ...
- python基础-条件语句if
if语句: if 判断条件: 执行语句 else: 执行语句 flag = Falsename = 'huipaodexiong'if name == 'python': flag = True ...
- JavaUtil_09_通用工具类-01_Hutool
一.重要的官方资料 1. Hutool 官网 2. Hutool 参考文档 3. Hutool API文档
- PHP之冒泡排序的优化
冒泡排序是一个常见的排序算法,它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成. 对于冒泡排序 ...
- 如何查看myeclipse是否激活
myEclipse---->Subscription information--->Subscription expiration date 看这个日期到什么时候!另外建议别用太高版本的M ...