I am developing with EF 4.3.1 CodeFirst. I have an Airport table as shown below:
public class Airport
{
[Key]
public int ID { get; set; }
public string Name{ get; set; }
}
What I need is a Route table with 2 FKs from the same Airport table like:
public class Route
{
public int DepartureAirportID { get; set; }
public int DestinationAirportID { get; set; }
public virtual Airport DestinationAirport { get; set; }
public virtual Airport DepartureAirport { get; set; }
}
How can this be achieved?
This should do what you need…
…this creates tables like…
…and you can use it like so…
…hope this helps.
Note: don’t use
virtualon required properties (as those are indexes – and for this type of mapping will only work like that, you’ll get some error I think).Also I always add the opposite relations but you can use WithMany() blank, should work too.