I understand that it is impossible in C#, but I need to do it somehow. I have two classes, a base class for list of flights.
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<Passenger> Passengers { get; set; }
public double PriceForFirstClass { get; set; }
public double PriceForBusiness { get; set; }
public double PriceForEconom { get; set; }
}
And derived class for archive list of flights.
class ArchiveFlight :Flight
{
}
Here is my DbContext class.
class FlightsDatabase : DbContext
{
protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Entity<Flight>().HasMany(x => x.Passengers).
WithOptional(x => x.Flight).HasForeignKey(x => x.flightNumber).WillCascadeOnDelete(true);
}
public DbSet<Flight> Flights { get; set;}
public DbSet<ArchiveFlight> FlightsArchive { get; set; }
public DbSet<Passenger> Passengers { get; set; }
}
}
I need to do something like this.
flights.FlightsArchive.Add((ArchiveFlight)flightToclean);
flights.Flights.Remove(flightToclean);
Where flightToclean is type of Flight. flights is type of FlightsDatabase .
Can I do this shortly or do I need to create new ArchiveFlight object and copy all properties of Flight object to new ArchiveFlight object?
ArchiveFlightthe base class. Or map CUD actions to stored procedures and in the delete procedure of aFlight, copy the deleted flight to theArchiveFlighttable.