Search code examples
entity-framework-6foreign-key-relationship

Are ForeignKey and InverseProperty attributes always applied on the many side in EF6.x


I am using Entity Framework 6.0 code-first approach in my project.

I have two entities - entity1 and entity2 that have a one-to-many relationship, so that one instance of entity1 can have one or more instances of entity2 and one instance of entity2 has only a single instance of entity1.

Question: For above scenario, will ForeignKey attribute always be mentioned in the definition of entity2 class, or it could also be specified in entity1 class definition? Similarly, is there a hard and fast rule for InverseProperty attribute that it must always be specified for entity2?

UPDATE 1

It seems that ForeignKey must always be mentioned in the entity that is on the many side of a relationship i.e. dependent entity, while InverseProperty must always be specified in the entity on the one side of a relationship i.e. principal entity. But, I am not sure.


Solution

  • ForeignKeyAttribute

    The annotation may be placed on the foreign key property and specify the associated navigation property name, or placed on a navigation property and specify the associated foreign key name.

    For a one-to-many relationship, only the entity on the "many" side has a foreign key to begin with, so it could only be used on that side.

    InversePropertyAttribute

    Specifies the inverse of a navigation property that represents the other end of the same relationship.

    A relationship has two ends, and the attribute can be placed on either end.

    • For a one-to-many relationship, here's an example of it placed on the "one" end:

      public class Post 
      { 
          public Person CreatedBy { get; set; } 
          public Person UpdatedBy { get; set; }
      }
      
      public class Person 
      { 
          public int Id { get; set; } 
          public string Name { get; set; } 
      
          [InverseProperty("CreatedBy")] 
          public List<Post> PostsWritten { get; set; } 
      
          [InverseProperty("UpdatedBy")] 
          public List<Post> PostsUpdated { get; set; }
      }
      

      https://msdn.microsoft.com/en-us/data/jj591583.aspx#Relationships

    • Here's an example on the "many" side:

      [Table("Department", Schema = "dbo")]  
      public class DepartmentMaster  
      {  
          public ICollection<Employee> PrimaryEmployees { get; set; }  
          public ICollection<Employee> SecondaryEmployees { get; set; }  
      }  
      
      [Table("Employee", Schema = "dbo")]  
      public class Employee  
      {  
          [InverseProperty("PrimaryEmployees")]  
          public DepartmentMaster PrimaryDepartment { get; set; }  
      
          [InverseProperty("SecondaryEmployees")]  
          public DepartmentMaster SecondaryDepartment { get; set; }  
      }  
      

      http://www.c-sharpcorner.com/UploadFile/ff2f08/entity-framework-code-first-data-annotations/