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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix