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 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-10-03 07:20:24

Entity Framework Code-First(9.11):DataAnnotations - InverseProperty Attribute的相关文章

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

Entity Framework Code First (三)Data Annotations

Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表示 EF 所依赖的模型去执行查询.更改追踪.以及更新功能,这意味着你的 domain classes 必须遵循 EF 所使用的约定.然而,如果你的 domain classes 不能遵循 EF 所使用的约定,此时你就需要有能力去增加一些配置使得你的 classes 能够满足 EF 所需要的信息. C

Entity Framework Code First (二)Custom Conventions

------------------------------------------------------------------------------------------------------------ 注意:以下所讨论的功能或 API 等只针对 Entity Framework 6 ,如果你使用早期版本,可能部分或全部功能不起作用! --------------------------------------------------------------------------