Entity Framework Model builder with conditioanl foreign key

c# entity-framework entity-framework-6


I'm now working on a legacy system database, which is a disaster. Based on systemid, the Reservation table in the hotel booking system table has a one-to-many relationship with various reservation systems (such as hotel reservations and railway reservations).
Reservation for a table
Columns Customer id, System id, and Reservation id
the number 45432 | 1, 2, 3, etc. | 343 |

One example of a system id is "hotel," "train," etc.

Table - [System] Hotel, rail, or aircraft reservations systems
Column Reservation id (int) | Create date (datetime) | Additional columns: 45432 |

Currently, we query the reservation database to load reservation information before querying the appropriate reservation system table depending on system id. Each reservation may have a connection with the reservation system ranging from 0..1* to many. I need to know how to develop an EF model builder around this. I tried these things

    public class HotelReservation
    public int Reservation_id { get; set; }
    public Virtual Reservation ReservationInfo { get; set; }
    //other properties etc
    public class Reservation
    public Reservation()
        HotelReservations  = new HashSet<HotelReservation>();
        TrainReservations  = new HashSet<TrainReservation>();
    public int Reservation_id { get; set; }
    public int SystemID { get; set; }
    public virtual ICollection<HotelReservation> HotelReservations { get; set; }
    public virtual ICollection<TrainReservation> TrainReservations { get; set; }
    //other properties etc
//On model builder class contains
            .HasOptional(x => x.ReservationInfo)
            .WithMany(x => x.TrainReservations )
            .HasForeignKey(x => x.Reservation_id);

however during startup, the error that follows Multiplicity clashes with Role's referential restriction. The Dependent Role's qualities cannot all be nullified, hence the Principal Role's multiplicity must be '1'.

8/29/2018 1:09:04 AM

Popular Answer

This seems to be an approximately Table-per-Type inheritance architecture, but that System Id appears to be an effort at Table-per-Hierarchy. You should be able to map the sub-class tables (Hotel Reservation, for example) Table-per-Type and completely disregard the SystemId in the relationship mapping if they have a Reservation ID. Basically, you should consider Reservation to be a basic class that other reservation kinds extend. You need to be able to query the basic reservation or particular reservation kinds from there.

Click here to read: https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt

It also describes various inherited data structure mappings, such as Table-per-Hierarchy, and how relationships are put up in EF.

8/29/2018 2:44:40 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow