简体   繁体   English

如何在一个表中创建两个指向某个表中同一列的外键?

[英]How to Create two Foreign Keys in one Table pointing to the same Column in some table?

I have a database design I want to implement, which is hard and make the error "there were multiple ForeignKeyAttributes which are pointing to same set of propertie" 我有一个我要实现的数据库设计,这很困难,并出现错误“有多个ForeignKeyAttributes指向相同的属性集”

we have an airport and flights, a flight have a Foreign key to the airport that it's flying from, and another Foreign key to the airport the flight is going to. 我们有一个机场和一个航班,一个航班有一个外键指向要飞行的机场,另一个有外键指向该航班要去的机场。

but both airports are in the same table. 但两个机场都在同一张表中。 there for the Flight table has two foreign key pointing at the same column in the Airport table Flight表中有两个外键指向Airport表中的同一列

the Airport Class 机场舱

{
public class Airport
{
    [Key]
    public int Id { get; set; }

    [ForeignKey(nameof(Flight.ToAirportId))]
    public ICollection<Flight> ComingFlightsId { get; set; }

    [ForeignKey(nameof(Flight.FromAirportId))]
    public ICollection<Flight> GoingFlightsId { get; set; }
}}

the flight class 飞行舱

{
public class Flight
{
    [Key]
    public int Id { get; set; }

    [ForeignKey(nameof(FromAirportId))]
    public Airport FromAirport { get; set; }
    public int FromAirportId { get; set; }

    [ForeignKey(nameof(ToAirportId))]
    public Airport ToAirport { get; set; }
    public int ToAirportId { get; set; }
}}

Just use this : 只需使用这个:

public class Airport
{
  public int Id { get; set; }

  [InverseProperty("FromAirport")]
  public ICollection<Flight> ComingFlights { get; set; }

  [InverseProperty("ToAirport")]
  public ICollection<Flight> GoingFlights { get; set; }
}


public class Flight
{
   public int Id { get; set; }

   public int FromAirportId { get; set; }

   public int ToAirportId { get; set; }

   [ForeignKey(nameof(FromAirportId))]
   [InverseProperty("ComingFlights")]
   public Airport FromAirport { get; set; }


   [ForeignKey(nameof(ToAirportId))]
   [InverseProperty("GoingFlights")]
   public Airport ToAirport { get; set; }    
 }

I found an answer to my question. 我找到了问题的答案。 Inverse Property 逆属性

add this annotation to the ICollection 将此注释添加到ICollection

this how the Airport class should look like in my case 这就是我的机场班级的样子

{
public class Airport
{
    [Key]
    public int Id { get; set; }
    public string Locatoin { get; set; }


    [InverseProperty(nameof(Flight.ToAirportId))]
    public ICollection<Flight> ComingFlightsId { get; set; }

    [InverseProperty(nameof(Flight.FromAirportId))]
    public ICollection<Flight> GoingFlightsId { get; set; }
}}

Try to use virtual modifier on public ICollection<Flight> ComingFlightsId { get; set; } 尝试在public ICollection<Flight> ComingFlightsId { get; set; }上使用virtual修饰符public ICollection<Flight> ComingFlightsId { get; set; } public ICollection<Flight> ComingFlightsId { get; set; } public ICollection<Flight> ComingFlightsId { get; set; } and public ICollection<Flight> GoingFlightsId { get; set; } public ICollection<Flight> ComingFlightsId { get; set; }public ICollection<Flight> GoingFlightsId { get; set; } public ICollection<Flight> GoingFlightsId { get; set; } public ICollection<Flight> GoingFlightsId { get; set; } and remove ForeignKey attribute from them public ICollection<Flight> GoingFlightsId { get; set; }然后从其中删除ForeignKey属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建一个表有两个外键使用实体框架代码首先指向同一个表 - How to create a table with two foreign keys pointing to the same table using Entity Framework Code First 实体框架-表需要两个指向同一表的外键 - Entity Framework - table needs two foreign keys pointing at same table 如何创建两个指向同一唯一字符串列的字符串外键? - How to create 2 string foreign keys pointing at the same unique string column? 如果table1包含fk到table2的同一列的多个字段,如何从一个表创建多个外键到另一个表 - How to create multiple foreign keys from one table to another if table1 contains multiple fields with fk to the same column of table2 实体框架DbContext混淆当两个外键指向同一张表时 - Entity Framework DbContext Confusion When two foreign keys pointing at same table 指向实体框架中相同表的多个外键是无法区分的 - Multiple foreign keys pointing to same table in Entity Framework are indistinguisible 3个外键到一个表到一列 - 3 foreign keys to one table to one column EF 6如何将两个外键设置为同一个表 - EF 6 how to set two foreign keys to same table 如何在EF中的同一个class(表)中添加两个外键 - How to add two foreign keys in the same class (table) in EF EF Core中同一个表的两个外键 - Two foreign keys to the same table in EF Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM