gpt4 book ai didi

c# - Entity Framework 中的反向属性和外键有什么区别?

转载 作者:可可西里 更新时间:2023-11-01 08:07:50 26 4
gpt4 key购买 nike

我知道当类之间有多个关系时会使用反向属性。但我对反向属性和外键属性感到困惑,因为它们都用于定义关系。

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. 指示与给定外键属性相关的导航属性的名称

    // 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; }
  2. 或指示给定导航属性的外键属性名称:

    // 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 代码优先约定不适用或以不适合您的方式应用时,它很有用。 Here您可以看到 EF 代码优先约定的列表。

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

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 外键。为了引导它,我们必须使用反向属性属性:

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 理解了我们的意图,将只创建两个外键,尽管名称不好。要同时更改列名,我们可以使用外键属性:

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 可以根据代码约定推断出很多东西。但是,当它不能时(例如,当您在同一个类中有两个外键时)- 您必须使用问题中的属性来帮助它。

关于c# - Entity Framework 中的反向属性和外键有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40480601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com