In a database I have Reservations and OldReservations tables, where OldReservations is a copy ( of Reservations table ) and is used to store old reservations. Here are the steps I did to create TPC inheritance:
1) I derived OldReservation entity from Reservations entity
2) I removed the overlapping properties from OldReservations entity
3) I’ve then mapped the OldReservations table fields in the XML of the EDMX file
But when I run the folllowing query, the generated sql uses LEFT OUTER JOIN, which doesn’t make sense, since it means that query will only return rows from OldReservations table where Reservations.ReservationID == OldReservations.ReservationID. From the articles I’ve read it seems that the above query should use an UNION operator and not the LEFT OUTER JOIN:
var reservations = context.Reservations;
foreach (var item in reservations);
Generated SQL:
SELECT CASE
WHEN (NOT (([Project1].[C1] = 1)
AND ([Project1].[C1] IS NOT NULL))) THEN '0X'
ELSE '0X0X'
END AS [C1],
[Extent1].[ReservationID] AS [ReservationID],
[Extent1].[ReservationDate] AS [ReservationDate],
[Extent1].[ContactID] AS [ContactID],
[Extent1].[EventID] AS [EventID],
[Extent1].[RowVersion] AS [RowVersion]
FROM [dbo].[Reservations] AS [Extent1]
LEFT OUTER JOIN (SELECT [Extent2].[ReservationID] AS [ReservationID],
cast(1 as bit) AS [C1]
FROM [dbo].[OldReservations] AS [Extent2]) AS [Project1]
ON [Extent1].[ReservationID] = [Project1].[ReservationID]
TPC select query using left outer join doesn’t make sense, so what am I doing wrong when creating TPC inheritance?
thank you
That’s how TPC is supposed to work.
Reservationis a concrete type, so the properties inReservationare stored in theReservationstable.OldReservationis also a concrete type, so the properties inOldReservationthat aren’t already mapped are stored in theOldReservationstable.What you want is similar, but not quite the same. Make
Reservationan abstract type and rename it toReservationBase. Then create a derived typeReservation. NowOldReservationno longer derives from a concrete type, so all ofOldReservationown properties and inherited properties will map to the OldReservations table.After that,
yourContext.ReservationBasescan be used to get bothReservationsandOldReservations, and that should use aUNION (ALL).