'Multiple Objects stored in database ASP.NET Entity Framework
Can I make the below changes to my databse whilst maintaining the one-to-many relationship? Would my Airport class need to change at all?
Existing Flight
class:
public class Flight
{
public int FlightId { get; set; }
public int DepartureAirportId { get; set; }
public int ArrivalAirportId { get; set; }
public DateTime FlightDateTime { get; set; }
public int FlightDurationHours { get; set; }
public Airport Airport { get; set; }
}
Proposed new Flight
class:
public class Flight
{
public int FlightId { get; set; }
public Airport DepartureAirport { get; set; }
public Airport ArrivalAirport { get; set; }
public DateTime FlightDateTime { get; set; }
public int FlightDurationHours { get; set; }
}
Airport class:
public class Airport
{
public int AirportId { get; set; }
public string AirportCode { get; set; }
//[ForeignKey("DepartureAirportId")]
public ICollection<Flight> Flight { get; set; }
}
I'm unsure if having two objects of Airport in the same model will make the one-to-many relationships fail?
Solution 1:[1]
Making changes to "production" databases with relationships and foreign keys via Entity Framework (or similar frameworks) is hard when you are making quite fundamental changes.
You really need to understand the underlying DB changes and table structures involved. Although you can abstract the DB when using EF and see a code view to a good extent, when making changes relying on a framework to make those those changes for you to your REAL data is something I have personally leaned away from.
I have posted about this topic before - Change the IDENTITY property of a column, the column needs to be dropped and recreated
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Cueball 6118 |