Share via

EntityFramework with multiple value

James 1 Reputation point
2021-12-12T10:31:26.757+00:00

Hi

I want to create a Code First EntityFramework Project/Database.

What I want is this (simplfied)

   public class Flight
    {
        public int Id { get; set; }
        public string FlightNumber { get; set; }
        public Airport Origin { get; set; }
        public Airport Destination { get; set; }
    }

    public class Airport
    {
        public string IataCode { get; set; }
        public string IcaoCode { get; set; }
        public string Name { get; set; }
    }

    public class MyContext : DbContext
    {
        public DbSet<Airport> Airports { get; set; }
        public DbSet<Flight> Flights { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Data Source=(localdb)\ProjectsV13;Initial Catalog=FlightsDB;");
        }
    }

But this doesn't work, as soon as I have 2 Airports in Flights.

How can I solve this?

I want to have finally 2 Databases with the "IataCode" as Identity (these are always a string with length 3 (ZRH, JFK, LAX).

Should I have a string IataCode in Flights? but how to link them?

Thanks - I am a beginner as you might have noticed :-D

Developer technologies | .NET | Entity Framework Core

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.