简体   繁体   English

当子记录不存在时,为什么Entity Framework(带有RIA Services)不包括父记录?

[英]Why is Entity Framework (with RIA Services) excluding parent records when child records don't exist?

Entity Framework 4 is excluding records that I don't believe it should be. 实体框架4排除了我认为不应该的记录。 Here is my scenario: 这是我的情况:

Table Definitions 表定义

Table_1

    UniqueIdentifier ID not null
    int AnotherField

Table_2

    UniqueIdentifier ID not null
    UniqueIdentifier Table_1ID not null
    int Priority not null

There is a relationship between table 1 and table 2 through the table_1ID field. 通过table_1ID字段在表1和表2之间存在关系。 It's defined in the database and the entity framework recognizes it. 它在数据库中定义,并且实体框架可以识别它。

I have a Query defined in my DomainService as: 我在DomainService中定义的查询为:

private ObjectQuery<Table_1> Table_1WithIncludes()    
{        
    return this.ObjectContext.Table_1        
        .Include("Table_2")                
}

If I have a record in table 1 and table 2 that are related, they get returned as expected. 如果我在表1和表2中有相关记录,它们将按预期返回。 If I do NOT have a record in table 2 that relates back to table 1, then the record is getting excluded. 如果我在表2中没有与表1相关的记录,那么该记录将被排除在外。

Upon running SQL Server Profiler, I noticed that the entity framework added the following CAST and where clauses: 运行SQL Server Profiler后,我注意到实体框架添加了以下CAST和where子句:

CASE WHEN ([Join2].[Priority] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]

WHERE ([Project1].[C1] > 0) 

So essentially, if the Priority field in Table_2 is null (which it is when there isn't a record in Table 2 that corresponds to the parent Tabe_1 table) the where clause causes both records to drop out (null > 0 fails). 因此,从本质上讲,如果Table_2中的Priority字段为null(当表2中没有与父Tabe_1表相对应的记录时就是这种情况),where子句会导致两条记录都丢失(null> 0失败)。

Now, if I change the definition of the Priority field so that it is not required, it eliminates the cast and the check and all is well. 现在,如果我更改“优先级”字段的定义,使它不是必需的,则它消除了强制类型转换和检查,一切都很好。 But it doesn't seem like I should have to do this. 但这似乎不应该我要做。

Have I done something wrong? 我做错什么了吗? Is my understanding faulty? 我的理解有误吗?

The full EntityQuery and generated query text is below in case it is helpful. 如果有帮助,请在下面查看完整的EntityQuery和生成的查询文本。

    private ObjectQuery<Event> EventsWithIncludes()
    {
        return this.ObjectContext.Events
            .Include("Place")
            .Include("EventInvitees")
            .Include("EventInvitees.User");
    }


exec sp_executesql N'SELECT 
[Project2].[NumberOfPeople] AS [NumberOfPeople], 
[Project2].[ID] AS [ID], 
[Project2].[CreatorID] AS [CreatorID], 
[Project2].[CreateDate] AS [CreateDate], 
[Project2].[PlacesID] AS [PlacesID], 
[Project2].[EventDate] AS [EventDate], 
[Project2].[EventTime] AS [EventTime], 
[Project2].[Availability] AS [Availability], 
[Project2].[EscalationLevels] AS [EscalationLevels], 
[Project2].[Rank] AS [Rank], 
[Project2].[RowVersion] AS [RowVersion], 
[Project2].[ID1] AS [ID1], 
[Project2].[Name] AS [Name], 
[Project2].[Phone] AS [Phone], 
[Project2].[DefaultPar] AS [DefaultPar], 
[Project2].[DefaultSlope] AS [DefaultSlope], 
[Project2].[DefaultRating] AS [DefaultRating], 
[Project2].[CreateDate1] AS [CreateDate1], 
[Project2].[UpdateDate] AS [UpdateDate], 
[Project2].[MetroAreaID] AS [MetroAreaID], 
[Project2].[C1] AS [C1], 
[Project2].[PriorityOrder] AS [PriorityOrder], 
[Project2].[ID2] AS [ID2], 
[Project2].[EventsID] AS [EventsID], 
[Project2].[InviteeUsersID] AS [InviteeUsersID], 
[Project2].[RowVersion1] AS [RowVersion1], 
[Project2].[Attending] AS [Attending], 
[Project2].[StatusChange] AS [StatusChange], 
[Project2].[ID3] AS [ID3], 
[Project2].[First] AS [First], 
[Project2].[Last] AS [Last], 
[Project2].[UserName] AS [UserName], 
[Project2].[Password] AS [Password], 
[Project2].[Gender] AS [Gender], 
[Project2].[Email] AS [Email], 
[Project2].[Email_Sharing] AS [Email_Sharing], 
[Project2].[Email_Receive] AS [Email_Receive], 
[Project2].[Phone1] AS [Phone1], 
[Project2].[Phone_Sharing] AS [Phone_Sharing], 
[Project2].[Phone_Receive] AS [Phone_Receive], 
[Project2].[CreateDate2] AS [CreateDate2], 
[Project2].[UpdateDate1] AS [UpdateDate1]
FROM ( SELECT 
    [Project1].[ID] AS [ID], 
    [Project1].[CreatorID] AS [CreatorID], 
    [Project1].[CreateDate] AS [CreateDate], 
    [Project1].[NumberOfPeople] AS [NumberOfPeople], 
    [Project1].[PlacesID] AS [PlacesID], 
    [Project1].[EventDate] AS [EventDate], 
    [Project1].[Availability] AS [Availability], 
    [Project1].[EscalationLevels] AS [EscalationLevels], 
    [Project1].[Rank] AS [Rank], 
    [Project1].[RowVersion] AS [RowVersion], 
    [Project1].[EventTime] AS [EventTime], 
    [Extent3].[ID] AS [ID1], 
    [Extent3].[Name] AS [Name], 
    [Extent3].[Phone] AS [Phone], 
    [Extent3].[DefaultPar] AS [DefaultPar], 
    [Extent3].[DefaultSlope] AS [DefaultSlope], 
    [Extent3].[DefaultRating] AS [DefaultRating], 
    [Extent3].[CreateDate] AS [CreateDate1], 
    [Extent3].[UpdateDate] AS [UpdateDate], 
    [Extent3].[MetroAreaID] AS [MetroAreaID], 
    [Join2].[ID1] AS [ID2], 
    [Join2].[EventsID] AS [EventsID], 
    [Join2].[InviteeUsersID] AS [InviteeUsersID], 
    [Join2].[PriorityOrder] AS [PriorityOrder], 
    [Join2].[RowVersion] AS [RowVersion1], 
    [Join2].[Attending] AS [Attending], 
    [Join2].[StatusChange] AS [StatusChange], 
    [Join2].[ID2] AS [ID3], 
    [Join2].[First] AS [First], 
    [Join2].[Last] AS [Last], 
    [Join2].[UserName] AS [UserName], 
    [Join2].[Password] AS [Password], 
    [Join2].[Gender] AS [Gender], 
    [Join2].[Email] AS [Email], 
    [Join2].[Email_Sharing] AS [Email_Sharing], 
    [Join2].[Email_Receive] AS [Email_Receive], 
    [Join2].[Phone] AS [Phone1], 
    [Join2].[Phone_Sharing] AS [Phone_Sharing], 
    [Join2].[Phone_Receive] AS [Phone_Receive], 
    [Join2].[CreateDate] AS [CreateDate2], 
    [Join2].[UpdateDate] AS [UpdateDate1], 
    CASE WHEN ([Join2].[PriorityOrder] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
    FROM    (SELECT 
        [Extent1].[ID] AS [ID], 
        [Extent1].[CreatorID] AS [CreatorID], 
        [Extent1].[CreateDate] AS [CreateDate], 
        [Extent1].[NumberOfPeople] AS [NumberOfPeople], 
        [Extent1].[PlacesID] AS [PlacesID], 
        [Extent1].[EventDate] AS [EventDate], 
        [Extent1].[Availability] AS [Availability], 
        [Extent1].[EscalationLevels] AS [EscalationLevels], 
        [Extent1].[Rank] AS [Rank], 
    [Extent1].[RowVersion] AS [RowVersion], 
    [Extent1].[EventTime] AS [EventTime], 
    (SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[EventInvitees] AS [Extent2]
        WHERE ([Extent1].[ID] = [Extent2].[EventsID]) AND ([Extent2].[InviteeUsersID] = @p__linq__2)) AS [C1]
    FROM [dbo].[Events] AS [Extent1] ) AS [Project1]
LEFT OUTER JOIN [dbo].[Places] AS [Extent3] ON [Project1].[PlacesID] = [Extent3].[ID]
LEFT OUTER JOIN  (SELECT [Extent4].[ID] AS [ID1], [Extent4].[EventsID] AS [EventsID], [Extent4].[InviteeUsersID] AS [InviteeUsersID], [Extent4].[PriorityOrder] AS [PriorityOrder], [Extent4].[RowVersion] AS [RowVersion], [Extent4].[Attending] AS [Attending], [Extent4].[StatusChange] AS [StatusChange], [Extent5].[ID] AS [ID2], [Extent5].[First] AS [First], [Extent5].[Last] AS [Last], [Extent5].[UserName] AS [UserName], [Extent5].[Password] AS [Password], [Extent5].[Gender] AS [Gender], [Extent5].[Email] AS [Email], [Extent5].[Email_Sharing] AS [Email_Sharing], [Extent5].[Email_Receive] AS [Email_Receive], [Extent5].[Phone] AS [Phone], [Extent5].[Phone_Sharing] AS [Phone_Sharing], [Extent5].[Phone_Receive] AS [Phone_Receive], [Extent5].[CreateDate] AS [CreateDate], [Extent5].[UpdateDate] AS [UpdateDate]
    FROM  [dbo].[EventInvitees] AS [Extent4]
    INNER JOIN [dbo].[Users] AS [Extent5] ON [Extent4].[InviteeUsersID] = [Extent5].[ID] ) AS [Join2] ON [Project1].[ID] = [Join2].[EventsID]
WHERE ([Project1].[C1] > 0) AND ([Project1].[CreatorID] = @p__linq__0) AND (([Project1].[EventDate] IS NULL) OR ([Project1].[EventDate] >= @p__linq__1))
)  AS [Project2]
ORDER BY [Project2].[ID] ASC, [Project2].[ID1] ASC, [Project2].[C1] ASC',N'@p__linq__2     uniqueidentifier,@p__linq__0 uniqueidentifier,@p__linq__1 datetime2(7)',@p__linq__2='33BB8199-7B25-4B3A-B96D-044EB7DB70AE',@p__linq__0='33BB8199-7B25-4B3A-B96D-044EB7DB70AE',@p__linq__1='1900-01-01 00:00:00'

Table Definition 表定义

USE [TheGreen18]
GO

/****** Object:  Table [dbo].[EventInvitees]    Script Date: 03/17/2012 22:27:16 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[EventInvitees](
    [ID] [uniqueidentifier] NOT NULL,
    [EventsID] [uniqueidentifier] NOT NULL,
    [InviteeUsersID] [uniqueidentifier] NOT NULL,
    [PriorityOrder] [int] NOT NULL,
    [RowVersion] [timestamp] NULL,
    [Attending] [bit] NULL,
    [StatusChange] [datetime] NULL,
 CONSTRAINT [PK_EventInvitees] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[EventInvitees]  WITH CHECK ADD  CONSTRAINT [FK_EventInvitees_Events] FOREIGN KEY([EventsID])
REFERENCES [dbo].[Events] ([ID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[EventInvitees] CHECK CONSTRAINT [FK_EventInvitees_Events]
GO

ALTER TABLE [dbo].[EventInvitees]  WITH CHECK ADD  CONSTRAINT [FK_EventInvitees_Users] FOREIGN KEY([InviteeUsersID])
REFERENCES [dbo].[Users] ([ID])
GO

ALTER TABLE [dbo].[EventInvitees] CHECK CONSTRAINT [FK_EventInvitees_Users]
GO

If your foreign key is non null able, then your relation is one to many according to EF, some every child must have parent. 如果您的外键不能为空,那么根据EF,您的关系是一对多的,每个孩子必须有父母。 So theoretically you can not have child without parent. 因此,从理论上讲,没有父母就不能有孩子。

If you want to have child without parent, then your relation is zero or one to many, and your foreign key should be null able. 如果您想生一个没有父母的孩子,那么您的关系为零或一对多,并且您的外键应该为空。

What I've seen of Entity Framework a single Include is always translated as an outer join. 我对实体框架所见的单个“ Include始终被翻译为外部联接。 With multiple Includes, however, the order of the includes determines whether the joins are inner or outer. 但是,对于多个包含,包含的顺序确定连接是内部连接还是外部连接。

In your example I assume that "Place" is outer joined, whereas both "EventInvitees" and "User" are inner joined. 在您的示例中,我假定“地方”是外部联接的,而“ EventInvitees”和“用户”都是内部联接的。 (At least that's what happens when I do a similar query). (至少当我执行类似的查询时,会发生这种情况)。

If you would change the order into 如果您将订单更改为

.Include("EventInvitees")
.Include("EventInvitees.User")
.Include("Place")

"Place" would be inner joined and "EventInvitees" outer joined (again, based on my similar case). “地方”将内部连接,“事件邀请”外部将连接(同样,基于我的类似案例)。

I cannot find any documentation on the exact logic of this at MSDN, so your result may differ from mine. 我在MSDN上找不到任何有关此逻辑的文档,因此您的结果可能与我的不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM