博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataAnnotations - InverseProperty Attribute:
阅读量:7232 次
发布时间:2019-06-29

本文共 3089 字,大约阅读时间需要 10 分钟。

DataAnnotations - InverseProperty Attribute:

We have seen in the  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
CurrentStudents { get; set; } public ICollection
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
CurrentStudents { get; set; } [InverseProperty("PreviousStandard")] public ICollection
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
CurrentStudents { get; set; } [InverseProperty("PreviousStandard")] public ICollection
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.

转载地址:http://supfm.baihongyu.com/

你可能感兴趣的文章
由system.currentTimeMillis() 获得当前的时间
查看>>
复习日记-Linux项目发布
查看>>
The 'Microsoft Jet OLEDB 4.0 Provider' is not registered on the local machine
查看>>
Java 基础源码 switch语句判断指定月份属于一年中的哪个季度
查看>>
12px以下字体显示问题
查看>>
小程序滚动条 无法滚动BUG 解决方案
查看>>
cs108 04 oop design
查看>>
win7 打开方式不能添加程序
查看>>
EasyUI-panel 内嵌页面上的js无法被执行
查看>>
pycharm运行input输入字符串报错 NameError: name 'xxx' is not defined
查看>>
微信小程序rpx单位
查看>>
Javascript读写CSS属性
查看>>
58.com qiyi
查看>>
ORACLE批量导入图片到BLOB字段
查看>>
OpenCl工作组
查看>>
Angular 学习笔记——$interpolate
查看>>
Javascript模块化编程之Why
查看>>
2016/4/5 对象
查看>>
[反射]通用方法 命名空间,类,对象,属性
查看>>
递归的代价
查看>>