1

I have two entities: Municipality and RoadSemgents. Municipality is parent and RoadSegments is a child table. I need to have two foreign Keys in RoadSegments from the same table (Municipality).

 public class Municipality
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
}


public class RoadSegments
{
    [Key]
    public int ID { get; set; }
    //ForeignKeys
    public int CodeMunicipalityLeft_ID { get; set; }
    public int CodeMunicipalityRight_ID { get; set; }

    [ForeignKey("CodeMunicipalityLeft_ID ")]
    public Municipality CodeMunicipalityLeft { get; set; }    // LOOK HERE 
    [ForeignKey("CodeMunicipalityRight_ID ")]
    public Municipality CodeMunicipalityRight { get; set; }   // AND HERE
}

How do I manage this situation? I have read some posts here for foreign keys to multiple tables but none worked for me.

Holger Brandt
  • 4,324
  • 1
  • 20
  • 35
Drill
  • 169
  • 2
  • 3
  • 12

0 Answers0