Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 为什么使用TPC继承时选择查询使用左外部联接?_Entity Framework_Entity Framework 4.1 - Fatal编程技术网

Entity framework 为什么使用TPC继承时选择查询使用左外部联接?

Entity framework 为什么使用TPC继承时选择查询使用左外部联接?,entity-framework,entity-framework-4.1,Entity Framework,Entity Framework 4.1,在数据库中,我有Reservations和OldReservations表,其中OldReservations是Reservations表的副本,用于存储旧的保留。以下是我创建TPC继承的步骤: 1) 我从Reservationsentity 2) 我从OldReservations实体中删除了重叠属性 3) 然后,我在EDMX文件的XML中映射了OldReservations表字段 但是,当我运行下面的查询时,生成的sql使用左外部联接,这没有意义,因为这意味着该查询将只返回oldpreser

在数据库中,我有
Reservations
OldReservations
表,其中OldReservations是
Reservations
表的副本,用于存储旧的保留。以下是我创建TPC继承的步骤:

1) 我从
Reservations
entity
2) 我从
OldReservations
实体中删除了重叠属性
3) 然后,我在EDMX文件的XML中映射了
OldReservations
表字段

但是,当我运行下面的查询时,生成的sql使用左外部联接,这没有意义,因为这意味着该查询将只返回
oldpreservations
表中的行,其中
Reservations.ReservationID==oldpreservations.ReservationID
。从我读过的文章来看,上面的查询似乎应该使用UNION运算符,而不是左外联接:

        var reservations = context.Reservations;
        foreach (var item in reservations);
生成的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查询没有意义,所以在创建TPC继承时我做错了什么


谢谢

这就是TPC的工作原理<代码>保留是一种具体类型,因此
保留
中的属性存储在
保留
表中
OldReservation
也是一种具体类型,因此
OldReservation
中尚未映射的属性存储在
OldReservation
表中

你想要的是相似的,但不是完全相同的。将
Reservation
设为抽象类型,并将其重命名为
ReservationBase
。然后创建一个派生类型
保留
。现在,
OldReservation
不再从具体类型派生,因此所有
OldReservation
自己的属性和继承的属性都将映射到OldReservations表


在这之后,
yourContext.ReservationBases
可以用来获取
Reservations
OldReservations
,这应该使用一个
联合(ALL)

@user702769您的
OldReservations
是否仍然来自
Reservation
?如果是这样,则不应该是这样,它应该从
ReservationBase
@user702769派生另一种可能性,确保
ReservationBase
未映射到任何表。只有
Reservation
oldservation
应该是。@user702769我想我没有完全理解你。是否要将
保留
旧保留
的公共属性映射到不同的表?如果没有,那么为了在数据库级别获得正确的结果,您需要左连接,因此您得到的是正确的。如果这样做,则无法将
ReservationBase
映射到单个表,因为该表将取决于
ReservationBase
Reservation
还是
oldservation
@user702769,这取决于oldservations的基本类型(在您的情况下是ReservationsBase,在我的情况下是ReservationsBase)是具体(非抽象)类型,则该基类型中的属性可以映射到单个表(您的示例)或不同的表(我的示例)。他们都是TPC。TPC只指定模型映射到数据库的方式,而不指定模型的组织方式。@user702769请参见示例,
Entity1
是一个抽象基类,从中派生出
Entity2
Entity3
。基类属性
Field1
存储在
Entity2Set
Entity3Set
表中。这看起来像是对TPT继承的查询。很抱歉,我没有注意到您的答复。我不知道你在说什么?