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.
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法