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 ...
随机推荐
- 【Flask模板】include标签
# include标签:1. 这个标签相当于是直接将指定的模版中的代码复制粘贴到当前位置.2. `include`标签,如果想要使用父模版中的变量,直接用就可以了,不需要使用`with context ...
- P4773 红鲤鱼与绿鲤鱼
P4773 红鲤鱼与绿鲤鱼 暑假比赛的一个水题 总情况数:\(\dfrac{(a+b)!}{a!b!}\) 就是\(a+b\)条鲤鱼中选\(a\) or \(b\)的情况 反正我们会用完鲤鱼,则红鲤鱼 ...
- uboot 2013.01 代码简析(3)第二阶段初始化
u-boot第二阶段初始化内容的入口函数是_main,_main位于arch/arm/lib/crt0.S文件中: _main函数中先为调用board_init_f准备初始化环境(设置栈指针sp和并给 ...
- IIS部署网站(建站)
双击IIS图标,运行IIS服务器: 如下图所示: 2 IIS打开后,选中网站,然后右键,在弹出的菜单中,选择新建,然后选择网站:如下图所hi: 3 在弹出的窗口中,直接点击下一步:如下图所示: 4 ...
- django admin后台显示H5颜色选项卡
一般使用django后台,特别是有颜色的字段的时候避免手动输入一大推的颜色,可以使用h5颜色选项卡,这样用户就直接可以通过选项卡来选择自己所需的颜色.这个时候可以通过后台admin form来进行定制 ...
- vi 常用编辑命令
什么是vi: vi是Linux/Unix底下最常用的文本编辑器,可以理解为和Windows下的txt一样,咱们一般操作linux服务器的时候都是没有图形化界面的, 怎么移动光标,到哪个位置,替换修改什 ...
- Linux基本语法
Shell编程 摘要: Shell历史 Shell的作用是解释用户的命令,用户输入一条命令,Shell就解释执行一条,这条方式称为交互式(interactive),Shell还有一种执行命令的方式称为 ...
- Oracle忘记用户名密码
一.oracle 11g登录服务开启 成功安装Oracle 11g后,共有7个服务,这七个服务的含义分别为:1. Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写 ...
- Java 对象引用以及对象赋值
一.Vehicle veh1 = new Vehicle(); 通常这条语句执行的动作被称为创建一个对象,其实他包含了四个动作. 1.new Vehicle :表示在堆空间内创建了一个Vehicle ...
- CCSpriteBatchNode CCSpriteFrameCache
3.27 精灵集合类(CCSpriteBatchNode) //以纹理来创建一个精灵集合对象 static CCSpriteBatchNode* createWithTexture(CCTexture ...