I created 2 classes which represent entities for a database.
First entity:
public enum FlightStatus
{
checkIn,
gateClosed,
arrived,
departedAt,
unknown,
canceled,
expectedAt,
delayed,
InFlight
}
public class Flight
{
[Key]
public string FlightNumber { get; set; }
public DateTime Arrival { get; set; }
public DateTime Departure { get; set; }
public string CityOfArrival { get; set; }
public string CityOfDeparture { get; set; }
public char Terminal { get; set; }
public FlightStatus Status { get; set; }
public int Gate { get; set; }
public virtual ICollection<Passanger> PassengerList { get; set; }
public double PriceForFirstClass { get; set; }
public double PriceForBusiness { get; set; }
public double PriceForEconom { get; set; }
}
Second entity:
public enum Sex
{
M,
F
}
public enum FlightClass
{
First,
Business,
Econom,
}
public enum TicketType
{
OneWay,
TwoWay,
}
public class Passenger
{
[Key]
public string Passport { get; set; }
public string flightNumber { get; set; }
[ForeignKey("flightNumber")]
public virtual Flight flight { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public string Nationality { get; set; }
public DateTime DateOfbirthday { get; set; }
public Sex SexOfPassanger { get; set; }
public FlightClass ClassForPassanger { get; set; }
public TicketType TypeOfTicket { get; set; }
}
Then I created a DbContext
:
class FlightsDatabase : DbContext
{
public DbSet<Flight> Flights { get; set; }
public DbSet<Flight> FlightsArchive { get; set; }
public DbSet<Passenger> Passengers { get; set; }
}
When I try to use this code:
using (FlightsDatabase flights = new FlightsDatabase())
{
foreach (Flight flight in flights.Flights)
{
if (Math.Abs((flight.Departure - DateTime.Now).Days) <= 1)
{
timeCame(flight, null);
}
if (flight.Departure < DateTime.Now)
{
flightsToClean.Add(flight);
}
}
moveOldFlights(flightsToClean, null);
}
I get this error
Multiple object sets per type are not supported. The object sets 'Flights' and 'FlightsArchive' can both contain instances of type 'Project_Airline_Info.Models.Flight'.
So my question is how to create 2 DBsets with same generic type,in DbContext class.
The short answer is that you can't do this. Consider this line of code:
var flight = context.Set<Flight>().Where(f => f.FlightNumber == "123");
How does it know which set to use to get the data?
Probably the simplest workaround would be to inherit the Flight
class and use that for your other DbSet
:
public class ArchiveFlight : Flight
{
}
And your context:
public class FlightsDatabase :DbContext
{
public DbSet<Flight> Flights { get; set; }
public DbSet<ArchiveFlight> FlightsArchive { get; set; }
public DbSet<Passanger> Passengers { get; set; }
}
The bonus of doing this is that you can now add properties to your archived flights, such as the date it was archived:
public class ArchiveFlight : Flight
{
public DateTime DateArchived { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With