专栏首页c#开发者DataAnnotations - InverseProperty Attribute:

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.

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Asp.net Webform 使用Repository模式实现CRUD操作代码生成工具

    Asp.net Webform 使用Repository模式实现CRUD操作代码生成工具 介绍 该工具是通过一个github上的开源项目修改的原始作者https...

    阿新
  • 为什么nhibernate 不能保存on-to-many的结构

    下面是主类文件 Code namespace EasyTalk.Module {     /// <summary>     /// SiteAddre...

    阿新
  • 使用EF6简实现多租户的应用

    网上有好多解释,有些上升到了架构设计,让你觉得似乎非常高深莫测,特别是目前流行的ABP架构中就有提到多租户(IMustHaveTenant),其实说的简单一点就...

    阿新
  • .NET 云原生架构师训练营(模块二 基础巩固 EF Core 基础与配置)--学习笔记

    Entity Framework Core:https://docs.microsoft.com/zh-cn/ef/core/

    郑子铭
  • Entity Framework DataAnnotations

    DataAnnotation 特性由.NET 3.5中引进,给.NET中的类提供了一种添加验证的方式。但是在EF中它又可以对映射关系进行控制,相比较Fluent...

    aehyok
  • DDD实战进阶第一波(八):开发一般业务的大健康行业直销系统(实现经销商上下文领域层之POCO模型)

    从这篇文章开始,我们开始介绍大健康行业直销系统领域层的实现。 先简单讲下业务方面的需求:直销系统会有一个顶级的经销商,经销商的基本信息中包括经销商的名字、联系人...

    用户1910585
  • DDD实战进阶第一波(八):开发一般业务的大健康行业直销系统(实现经销商上下文领域层之POCO模型)

    用户1910585
  • .NET 云原生架构师训练营(模块二 基础巩固 EF Core 基础与配置)--学习笔记

    Entity Framework Core:https://docs.microsoft.com/zh-cn/ef/core/

    郑子铭
  • EntityFramework Core 学习扫盲

    0. 写在前面 本篇文章虽说是入门学习,但是也不会循规蹈矩地把EF1.0版本一直到现在即将到来的EF Core 2.0版本相关的所有历史和细节完完整整还原出来...

    潘成涛
  • 【C#】AutoMapper 使用手册

    官方文档:https://docs.automapper.org/en/latest/

    丹枫无迹

扫码关注云+社区

领取腾讯云代金券