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.

DataAnnotations - InverseProperty Attribute:的更多相关文章

  1. Entity Framework Code-First(9.11):DataAnnotations - InverseProperty Attribute

    DataAnnotations - InverseProperty Attribute: We have seen in the Code-First Convention section that ...

  2. Entity Framework Code-First(9.10):DataAnnotations - NotMapped Attribute

    DataAnnotations - NotMapped Attribute: NotMapped attribute can be applied to properties of a class. ...

  3. Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute

    DataAnnotations - ForeignKey Attribute: ForeignKey attribute can be applied to properties of a class ...

  4. Entity Framework Code-First(9.8):DataAnnotations - Column Attribute

    DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Defaul ...

  5. Entity Framework Code-First(9.7):DataAnnotations - Table Attribute

    DataAnnotations - Table Attribute: Table attribute can be applied to a class. Default Code-First con ...

  6. Entity Framework Code-First(9.6):DataAnnotations - StringLength Attribute

    DataAnnotations - StringLength Attribute: StringLength attribute can be applied to a string type pro ...

  7. Entity Framework Code-First(9.5):DataAnnotations - MaxLength Attribute

    DataAnnotations - MaxLength Attribute: MaxLength attribute can be applied to a string or array type ...

  8. Entity Framework Code-First(9.2):DataAnnotations - TimeStamp Attribute

    DataAnnotations - TimeStamp Attribute: TimeStamp attribute can be applied to only one byte array pro ...

  9. Entity Framework Code-First(9.1):DataAnnotations - Key Attribute

    DataAnnotations - Key Attribute: Key attribute can be applied to properties of a class. Default Code ...

随机推荐

  1. 201621123002《JAVA程序设计》第二周学习总结

    1.本周学习总结 1.重点String类 2.Java的数据类型 3.Java中的引用类,包装类 for(类型 元素变量名(任取):遍历对象(数组名)) 2.书面作业 1.String-使用Eclip ...

  2. 1-JRE与JDK等知识

  3. 2019.03.01 bzoj3075: [Usaco2013]Necklace(kmp+dp)

    传送门 题意简述:给出S,TS,TS,T两个字串,∣S∣≤10000,∣T∣≤1000|S|\le10000,|T|\le1000∣S∣≤10000,∣T∣≤1000,问至少从SSS中删去几个字符能够 ...

  4. Noxim Overview

    PE+Router= Tile Node Architectural Elements: Buffer.h, Router.h, LocalRoutingTable.h, Tile.h, NoC.h, ...

  5. Day02 (黑客成长日记)

    #用户登录次数为三代码 # i = 0 # while i < 3: # username = input('请输入账号:') # password = input('请输入密码:') # if ...

  6. Beta冲刺 (4/7)

    Part.1 开篇 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1(组长)柯奇豪 过去两天完成了哪些任务 共享编辑文章的后端数据处理 展示Gi ...

  7. 2nd week

    <!DOCTYPE html> <html> <head> <title>用户登录.html</title> <meta http-e ...

  8. 剑指offer编程题Java实现——面试题10二进制中1的个数

    题目: 请实现一个函数,输入一个整数,输出该整数二进制表示中1的个数.例如,把9表示成二进制是1001,有2位是1,该函数输出2解法:把整数减一和原来的数做与运算,会把该整数二进制表示中的最低位的1变 ...

  9. JVM活学活用——调优工具

    概述 工具做为图形化界面来展示更能直观的发现问题,另一方面一些耗费性能的分析(dump文件分析)一般也不会在生产直接分析,往往dump下来的文件达1G左右,人工分析效率较低,因此利用工具来分析jvm相 ...

  10. Swift5 语言指南(十三) 方法

    方法是与特定类型相关联的函数.类,结构和枚举都可以定义实例方法,这些方法封装了用于处理给定类型的实例的特定任务和功能.类,结构和枚举也可以定义类型方法,它们与类型本身相关联.类型方法类似于Object ...