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.

时间: 2024-12-30 02:03:57

DataAnnotations - InverseProperty Attribute:的相关文章

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

Entity Framework Code-First(9.8):DataAnnotations - Column Attribute

DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Default Code First convention creates a column name same as the property name. Column attribute overrides this default convention. EF Code-First will create

Entity Framework Code-First(9.2):DataAnnotations - TimeStamp Attribute

DataAnnotations - TimeStamp Attribute: TimeStamp attribute can be applied to only one byte array property of a domain class. TimeStamp attribute creates a column with timestamp datatype. Code-First automatically use this TimeStamp column in concurren

Entity Framework Code-First(9.7):DataAnnotations - Table Attribute

DataAnnotations - Table Attribute: Table attribute can be applied to a class. Default Code-First convention creates a table name same as the class name. Table attribute overrides this default convention. EF Code-First will create a table with a speci

Entity Framework Code-First(9.10):DataAnnotations - NotMapped Attribute

DataAnnotations - NotMapped Attribute: NotMapped attribute can be applied to properties of a class. Default Code-First convention creates a column for all the properties which includes getters and setters. NotMapped attribute overrides this default c

Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute

DataAnnotations - ForeignKey Attribute: ForeignKey attribute can be applied to properties of a class. Default Code-First convention for ForeignKey relationship expects foreign key property name match with primary key property. Consider the following

Entity Framework Code-First(9.1):DataAnnotations - Key Attribute

DataAnnotations - Key Attribute: Key attribute can be applied to properties of a class. Default Code-First convention creates a primary key column for a property whose name is "Id" or {Class Name} + "Id". Key attribute overrides this d

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 NULL column in a database table for a property on which we apply Required attribute. Note that it can also be used with ASP.Net MVC as a validation att

EF Code-First 学习之旅 DataAnnotations

数据注解:配置选项的子集:Fluent API包含所有选项 System.ComponentModel.DataAnnotations Attributes: Attribute Description Key 标记实体的属性映射到数据库表中的主键 Timestamp 标记助兴为不可空的时间戳列(行版本) ConcurrencyCheck 标记一个或多个属性做并发检查(当用户编辑或删除数据的时候)  Required 属性必须有值 MinLength 设置属性类型为数组或字符串的最小长度 Max