Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7750337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:15:55+00:00 2026-06-01T11:15:55+00:00

I am developing with EF 4.3.1 CodeFirst. I have an Airport table as shown

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:15:56+00:00Added an answer on June 1, 2026 at 11:15 am

    This should do what you need…

    public class Airport
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Route> DepartureRoutes { get; set; }
        public virtual ICollection<Route> DestinationRoutes { get; set; }
    }
    public class Route
    {
        public int DepartureAirportID { get; set; }
        public int DestinationAirportID { get; set; }
        public Airport DestinationAirport { get; set; }
        public Airport DepartureAirport { get; set; }
    }
    
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Route>()
            .HasKey(i => new { i.DepartureAirportID, i.DestinationAirportID});
    
        modelBuilder.Entity<Route>()
            .HasRequired(i => i.DepartureAirport)
            .WithMany(u => u.DepartureRoutes)
            .HasForeignKey(i => i.DepartureAirportID)
            .WillCascadeOnDelete(false);
    
        modelBuilder.Entity<Route>()
            .HasRequired(i => i.DestinationAirport)
            .WithMany(u => u.DestinationRoutes)
            .HasForeignKey(i => i.DestinationAirportID)
            .WillCascadeOnDelete(false);
    }
    

    …this creates tables like…

    CREATE TABLE [Airports] (
        [ID] [int] NOT NULL IDENTITY,
        [Name] [nvarchar](4000),
        CONSTRAINT [PK_Airports] PRIMARY KEY ([ID])
    )
    CREATE TABLE [Routes] (
        [DepartureAirportID] [int] NOT NULL,
        [DestinationAirportID] [int] NOT NULL,
        CONSTRAINT [PK_Routes] PRIMARY KEY ([DepartureAirportID], [DestinationAirportID])
    )
    CREATE INDEX [IX_DestinationAirportID] ON [Routes]([DestinationAirportID])
    CREATE INDEX [IX_DepartureAirportID] ON [Routes]([DepartureAirportID])
    ALTER TABLE [Routes] ADD CONSTRAINT [FK_Routes_Airports_DestinationAirportID] FOREIGN KEY ([DestinationAirportID]) REFERENCES [Airports] ([ID])
    ALTER TABLE [Routes] ADD CONSTRAINT [FK_Routes_Airports_DepartureAirportID] FOREIGN KEY ([DepartureAirportID]) REFERENCES [Airports] ([ID])
    

    …and you can use it like so…

    using (var db = new MyDbContext())
    {
        foreach (var routeid in Enumerable.Range(1, 100))
        {
            var departure = new Airport { Name = "departure" + routeid };
            db.Airports.Add(departure);
            var destination = new Airport { Name = "destination" + routeid };
            db.Airports.Add(destination);
    
            var route = new Route{ DepartureAirport = departure, DestinationAirport = destination };
            db.Routes.Add(route);
        }
    
        int recordsAffected = db.SaveChanges();
    
        foreach (var route in db.Routes)
        {
            Console.WriteLine("{0}, {1}, {2}, {3}", route.DepartureAirportID, route.DestinationAirportID, route.DepartureAirport.Name, route.DestinationAirport.Name);
        }
    }
    

    …hope this helps.
    Note: don’t use virtual on 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

developing iphone app, I have used a UIImageview and i have set an image
Developing a network application, I have a Connection class that manages sending and receiving
Developing ios app. I have an object class Product.h and .m respectively along with
Developing a project of mine I realize I have a need for some level
Developing for iPhone, I have a collection of points that I need to make
I have been developing the skeleton of a database that will support versioning of
Developing a Django web app, I have a list of packages I need to
Developing an MVC application, i now need to have test other browser versions. Installed
Developing a bridge to use a cms with MongoDB I use a storage key,
I'll let the code do the talking here, I have something like this: class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.