实体框架中逆属性与外键有什么区别? [英] what is difference between inverse property and foreign key in entity framework?

查看:96
本文介绍了实体框架中逆属性与外键有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在类之间具有多个关系时会使用逆属性。但是逆属性和外键属性使我感到困惑,因为它们都是用于定义关系的。

I knew that Inverse Property is used when you have multiple relationships between classes. but I am confused between inverse property and foreign key property since both of them are used for defining relationships.

public class PrivilegeToDbOperationTypeMap : BaseEntity
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Column(Order = 0)]
    public int PrivilegeToDbOperationTypeMapId { get; set; }

    [ForeignKey("privilegeLookup"), Column(Order = 1)]
    [Index("IX_PrivilegeLookupId_DbOperationLookupId", 1, IsUnique = true)]
    public int PrivilegeLookupId { get; set; }

    [ForeignKey("dbOperationTypeLookup"), Column(Order = 2)]
    [Index("IX_PrivilegeLookupId_DbOperationLookupId", 2, IsUnique = true)]
    public int DbOperationLookupId { get; set; }

    #region Navigation Properties

    public PrivilegeLookup privilegeLookup { get; set; }

    public DbOperationTypeLookup dbOperationTypeLookup { get; set; }

    [InverseProperty("privilegeToDbOperationTypeMap")]
    public ICollection<RoleToPrivilegeDbOperationTypeMap> roleToPrivilegeDbOperationTypeMaps { get; set; }

    #endregion Navigation Properties
}


推荐答案

外键属性用于:


  1. 指示与给定外键相关的导航属性的名称属性

  1. Indicate the name of navigation property related to given foreign key property

// this is foreign key property with related "privilegeLookup" navigation property. Database column name will be PrivilegeLookupId
[ForeignKey("privilegeLookup"), Column(Order = 1)]       
public int PrivilegeLookupId { get; set; }
// this is related navigation property
public PrivilegeLookup privilegeLookup { get; set; }


  • OR表示给定导航属性的外键属性的名称:

  • OR indicate the name of foreign key property for given navigation property:

    // this is foreign key property
    public int PrivilegeLookupId { get; set; }
    // this is navigation property with related foreign key property
    [ForeignKey("PrivilegeLookupId")]  
    public PrivilegeLookup privilegeLookup { get; set; }
    


  • 当默认EF代码-首要约定不适用,或以不适合您的方式适用。 此处,您可以看到EF代码优先约定的列表。

    It is useful when default EF code-first conventions do not apply, or apply in a way not suitable for you. Here you can see a list of EF code-first conventions.

    反向属性属性用于当您需要指示类 A 中的导航属性与另一个导航属性与同一外键相关时类 B 。例如:

    Inverse Property attribute is used when you need to indicate that navigation property in class A is related to the same foreign key as another navigation property in class B. For example:

    public class Student
    {
        public int StudentID { get; set; }
    
        public Standard CurrentStandard { get; set; }
        public Standard PreviousStandard { get; set; }
    }
    
    public class Standard
    {    
        public int StandardId { get; set; }
    
        public ICollection<Student> CurrentStudents { get; set; }
        public ICollection<Student> PreviousStudents { get; set; }   
    }
    

    在这里,我们有两个类,每个类都有两个导航属性。我们的意图是在表Student中具有两个外键,可能分别命名为CurrentStandardId和PreviousStandardId,并且Standard类的导航属性也与相同的外键相关(一对多关系)。但是,在这种情况下,如果没有其他指导,EF将无法实现,而是创建 4 外键。要进行指导,我们必须使用逆属性属性:

    Here we have two classes, each with two navigation properties. Our intention is to have two foreign keys in table Student, probably named CurrentStandardId and PreviousStandardId, and navigation properties of class Standard are also related to the same foreign keys (one to many relationship). However, in this case EF will not realize this without futher guidance - instead it will create 4 foreign keys. To guide it, we have to use inverse property attribute:

    public class Standard
    {
        public int StandardId { get; set; }
    
        // reference to the name of another navigation property in class Student
        [InverseProperty("CurrentStandard")]
        public ICollection<Student> CurrentStudents { get; set; }
    
        // reference to the name of another navigation property in class Student
        [InverseProperty("PreviousStandard")]
        public ICollection<Student> PreviousStudents { get; set; }   
    }
    

    现在,EF理解了我们的意图,将仅创建两个外键,尽管名字会不好。要更改列名,我们可以使用外键属性:

    Now EF understands our intentions and will create just two foreign keys, though the names will be not good. To also change column names, we can use foreign key attribute:

    public class Student 
    {
        public int StudentID { 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; }
    }
    

    长话短说-EF可以根据代码约定推断出很多东西。但是,如果它不能(例如,当您在同一个类中有两个外键时),则必须使用问题中的属性来帮助它。

    Long story short - EF can deduce many things based on code conventions. However when it cannot (for example when you have two foreign keys in the same class) - you have to help it using attributes from your question.

    这篇关于实体框架中逆属性与外键有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆