locked
What is inverse property and when to use it RRS feed

Answers

  • User1771544211 posted

    Hi tapan2017,

    Based on my understanding, the InverseProperty is used when you have type in one class and both has the relation with the current class like the following.

    public class Contact
    {
        public int ContactId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        // The CreateBy and UpdateBy are same type and both have the relationship with the User class
        public User CreatedBy { get; set; }
        public User UpdatedBy { get; set; }
    }

    When the EF code-first creates the database it will create {Class Name}_{Primary Key} foreign key column if you have not included foreign key property in a parent class.

    So it will create foreign key columns ( User_UserId, User_UserId2 ) in databases to map the CreatedBy and UpdatedBy. Which is not a good practice for managing the database and not easy the understand the meaning of the two properties.

    To avoid this, you can use the InverseProperty, thenthe EF code-first creates  {InverseProperty value}_{Primary Key} foreign key column in the datatable. So it will create foreign key columns(CreatedBy_UserId,  UpdatedBy_UserId ) when you use the InverseProperty like the following.

    [InverseProperty("CreatedBy")]    
    public User CreatedBy { get; set; }
    [InverseProperty("UpdatedBy")]
    public User UpdatedBy { get; set; }

    The InverseProperty is seldom used because of it must meet the above conditions when use this. And we often use the ForeignKey attribute to assign the foreign key column name directly instead of using InverseProperty like the following.

    [ForeignKey("CreatedById")]    
    public User CreatedBy { get; set; }
    [ForeignKey("UpdatedById")]
    public User UpdatedBy { get; set; }

    Then it will generate the foreign key colums (CreateById, UpdateById) in the datatable.

    Best Regards,

    Jean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 24, 2017 3:18 AM

All replies

  • User2119946224 posted

    Hello tapan2017,

    As per my understanding, it will help in code first approach when user want to create relationship between two tables.

    If you directly add property with other class it will creates wrong foreign key but if you use "InverseProperty" it will creates right keys for you.

    Means name wise it will named like which column mapped with which one. Also if you not placed foreign key and inverseproperty it will created dummy foreign key for you.

    Please see below link will help you to understand.

    https://stackoverflow.com/questions/10020660/why-does-ef-code-first-inverseproperty-attribute-fail-to-work-when-used-with

    Hope you get some understanding for this.

    Regards,

    Friday, July 21, 2017 12:20 PM
  • User828542847 posted

    not very clear the code i got from your link

    [Table("Matches")]
    public class Match
    {
        [Key]
        public long Id { get; set; }
    
        [ForeignKey("Player1Home")]
        public long? HPlayer1Id { get; set; }
    
        [InverseProperty("MatchesAsHome1")]
        public virtual Player Player1Home { get; set; }
    }
    
    [Table("Players")]
    public class Player
    {
        [Key]
        public long Id { get; set; }
    
        public virtual ICollection<Match> MatchesAsHome1 { get; set; }
    }

    can you make me understand with a other example for InverseProperty.

    Friday, July 21, 2017 12:31 PM
  • User1771544211 posted

    Hi tapan2017,

    Based on my understanding, the InverseProperty is used when you have type in one class and both has the relation with the current class like the following.

    public class Contact
    {
        public int ContactId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        // The CreateBy and UpdateBy are same type and both have the relationship with the User class
        public User CreatedBy { get; set; }
        public User UpdatedBy { get; set; }
    }

    When the EF code-first creates the database it will create {Class Name}_{Primary Key} foreign key column if you have not included foreign key property in a parent class.

    So it will create foreign key columns ( User_UserId, User_UserId2 ) in databases to map the CreatedBy and UpdatedBy. Which is not a good practice for managing the database and not easy the understand the meaning of the two properties.

    To avoid this, you can use the InverseProperty, thenthe EF code-first creates  {InverseProperty value}_{Primary Key} foreign key column in the datatable. So it will create foreign key columns(CreatedBy_UserId,  UpdatedBy_UserId ) when you use the InverseProperty like the following.

    [InverseProperty("CreatedBy")]    
    public User CreatedBy { get; set; }
    [InverseProperty("UpdatedBy")]
    public User UpdatedBy { get; set; }

    The InverseProperty is seldom used because of it must meet the above conditions when use this. And we often use the ForeignKey attribute to assign the foreign key column name directly instead of using InverseProperty like the following.

    [ForeignKey("CreatedById")]    
    public User CreatedBy { get; set; }
    [ForeignKey("UpdatedById")]
    public User UpdatedBy { get; set; }

    Then it will generate the foreign key colums (CreateById, UpdateById) in the datatable.

    Best Regards,

    Jean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 24, 2017 3:18 AM