コード例 #1
0
        [Fact] // CodePlex #2595
        public void Projecting_collection_inside_entity_is_null_comparable()
        {
            var expectedSql =
                @"SELECT 
    1 AS [C1], 
    [Extent1].[Id] AS [Id], 
    CASE WHEN ([Extent1].[Reference1_Id] IS NULL) THEN cast(1 as bit) ELSE cast(0 as bit) END AS [C2], 
    [Extent1].[Reference1_Id] AS [Reference1_Id], 
    CASE WHEN ([Extent1].[Reference2_Id] IS NULL) THEN cast(1 as bit) ELSE cast(0 as bit) END AS [C3], 
    [Extent1].[Reference2_Id] AS [Reference2_Id]
    FROM [dbo].[Entity1] AS [Extent1]";

            using (var ctx = new Issue2595Context())
            {
                ctx.Database.Delete();

                var entity1 = new Entity1
                {
                    Id         = "1",
                    Reference1 = new Entity2
                    {
                        Id         = "1.1",
                        Collection = new List <Entity3>()
                        {
                            new Entity3 {
                                Id = "1.1.1"
                            },
                            new Entity3 {
                                Id = "1.1.2"
                            }
                        }
                    },
                    Reference2 = new Entity4 {
                        Id = "1.2"
                    }
                };

                var entity2 = new Entity1 {
                    Id = "2", Reference1 = null, Reference2 = new Entity4 {
                        Id = "2.2"
                    }
                };
                var entity3 = new Entity1 {
                    Id = "3", Reference1 = null, Reference2 = null
                };

                ctx.Entities1.Add(entity1);
                ctx.Entities1.Add(entity2);
                ctx.Entities1.Add(entity3);
                ctx.SaveChanges();
            }

            using (var ctx = new Issue2595Context())
            {
                var query = ctx.Entities1.Include(e => e.Reference1).Include("Reference.Collection").Include(e => e.Reference2)
                            .Select(r => new { r.Id, IsNull1 = r.Reference1 == null, r.Reference1, IsNull2 = r.Reference2 == null, r.Reference2 });

                QueryTestHelpers.VerifyDbQuery(query, expectedSql);
            }
        }
コード例 #2
0
        [Fact] // CodePlex #2595
        public void Projecting_collection_inside_entity_is_null_comparable()
        {
            var expectedSql =
@"SELECT 
    1 AS [C1], 
    [Extent1].[Id] AS [Id], 
    CASE WHEN ([Extent1].[Reference1_Id] IS NULL) THEN cast(1 as bit) ELSE cast(0 as bit) END AS [C2], 
    [Extent1].[Reference1_Id] AS [Reference1_Id], 
    CASE WHEN ([Extent1].[Reference2_Id] IS NULL) THEN cast(1 as bit) ELSE cast(0 as bit) END AS [C3], 
    [Extent1].[Reference2_Id] AS [Reference2_Id]
    FROM [dbo].[Entity1] AS [Extent1]";

            using (var ctx = new Issue2595Context())
            {
                ctx.Database.Delete();

                var entity1 = new Entity1
                {
                    Id = "1",
                    Reference1 = new Entity2
                    {
                        Id = "1.1",
                        Collection = new List<Entity3>()
                        {
                            new Entity3 { Id = "1.1.1" },
                            new Entity3 { Id = "1.1.2" }
                        }
                    },
                    Reference2 = new Entity4 { Id = "1.2" }
                };

                var entity2 = new Entity1 { Id = "2", Reference1 = null, Reference2 = new Entity4 { Id = "2.2" } };
                var entity3 = new Entity1 { Id = "3", Reference1 = null, Reference2 = null };

                ctx.Entities1.Add(entity1);
                ctx.Entities1.Add(entity2);
                ctx.Entities1.Add(entity3);
                ctx.SaveChanges();
            }

            using (var ctx = new Issue2595Context())
            {
                var query = ctx.Entities1.Include(e => e.Reference1).Include("Reference.Collection").Include(e => e.Reference2)
                    .Select(r => new { r.Id, IsNull1 = r.Reference1 == null, r.Reference1, IsNull2 = r.Reference2 == null, r.Reference2 });

                QueryTestHelpers.VerifyDbQuery(query, expectedSql);
            }
        }
コード例 #3
0
        [Fact] // CodePlex #2595
        public void Projecting_collection_of_DTOs_inside_DTO_is_null_comparable()
        {
            var expectedSql =
                @"SELECT 
    [Project1].[C1] AS [C1], 
    [Project1].[Id] AS [Id], 
    [Project1].[C2] AS [C2], 
    [Project1].[Reference1_Id] AS [Reference1_Id], 
    [Project1].[C3] AS [C3], 
    [Project1].[Reference2_Id] AS [Reference2_Id], 
    [Project1].[C4] AS [C4], 
    [Project1].[Id1] AS [Id1]
    FROM ( SELECT 
        [Extent1].[Id] AS [Id], 
        [Extent1].[Reference1_Id] AS [Reference1_Id], 
        [Extent1].[Reference2_Id] AS [Reference2_Id], 
        1 AS [C1], 
        cast(0 as bit) AS [C2], 
        cast(0 as bit) AS [C3], 
        [Extent2].[Id] AS [Id1], 
        CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C4]
        FROM  [dbo].[Entity1] AS [Extent1]
        LEFT OUTER JOIN [dbo].[Entity3] AS [Extent2] ON [Extent2].[Entity2_Id] = [Extent1].[Reference1_Id]
    )  AS [Project1]
    ORDER BY [Project1].[Id] ASC, [Project1].[C4] ASC";

            using (var ctx = new Issue2595Context())
            {
                ctx.Database.Delete();

                var entity1 = new Entity1
                {
                    Id         = "1",
                    Reference1 = new Entity2
                    {
                        Id         = "1.1",
                        Collection = new List <Entity3>()
                        {
                            new Entity3 {
                                Id = "1.1.1"
                            },
                            new Entity3 {
                                Id = "1.1.2"
                            }
                        }
                    },
                    Reference2 = new Entity4 {
                        Id = "1.2"
                    }
                };

                var entity2 = new Entity1 {
                    Id = "2", Reference1 = null, Reference2 = new Entity4 {
                        Id = "2.2"
                    }
                };
                var entity3 = new Entity1 {
                    Id = "3", Reference1 = null, Reference2 = null
                };

                ctx.Entities1.Add(entity1);
                ctx.Entities1.Add(entity2);
                ctx.Entities1.Add(entity3);
                ctx.SaveChanges();
            }

            using (var ctx = new Issue2595Context())
            {
                var query = ctx.Entities1.Select(e => new DTO1
                {
                    Id         = e.Id,
                    Reference1 = new DTO2
                    {
                        Id         = e.Reference1.Id,
                        Collection = e.Reference1.Collection.Select(c => new DTO3 {
                            Id = c.Id
                        })
                    },
                    Reference2 = new DTO4 {
                        Id = e.Reference2.Id
                    }
                }).Select(r => new { r.Id, IsNull1 = r.Reference1 == null, r.Reference1, IsNull2 = r.Reference2 == null, r.Reference2 });

                QueryTestHelpers.VerifyDbQuery(query, expectedSql);
                var results = query.ToList();

                Assert.Equal(3, results.Count);
                Assert.True(results.Select(r => r.IsNull1).All(r => !r));
                Assert.True(results.Select(r => r.IsNull2).All(r => !r));
                Assert.True(results.Select(r => r.Reference1).All(r => r != null));
                Assert.True(results.Select(r => r.Reference2).All(r => r != null));
            }
        }
コード例 #4
0
        [Fact] // CodePlex #2595
        public void Projecting_collection_of_DTOs_inside_DTO_is_null_comparable()
        {
            var expectedSql =
@"SELECT 
    [Project1].[C1] AS [C1], 
    [Project1].[Id] AS [Id], 
    [Project1].[C2] AS [C2], 
    [Project1].[Reference1_Id] AS [Reference1_Id], 
    [Project1].[C3] AS [C3], 
    [Project1].[Reference2_Id] AS [Reference2_Id], 
    [Project1].[C4] AS [C4], 
    [Project1].[Id1] AS [Id1]
    FROM ( SELECT 
        [Extent1].[Id] AS [Id], 
        [Extent1].[Reference1_Id] AS [Reference1_Id], 
        [Extent1].[Reference2_Id] AS [Reference2_Id], 
        1 AS [C1], 
        cast(0 as bit) AS [C2], 
        cast(0 as bit) AS [C3], 
        [Extent2].[Id] AS [Id1], 
        CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C4]
        FROM  [dbo].[Entity1] AS [Extent1]
        LEFT OUTER JOIN [dbo].[Entity3] AS [Extent2] ON [Extent2].[Entity2_Id] = [Extent1].[Reference1_Id]
    )  AS [Project1]
    ORDER BY [Project1].[Id] ASC, [Project1].[C4] ASC";

            using (var ctx = new Issue2595Context())
            {
                ctx.Database.Delete();

                var entity1 = new Entity1
                {
                    Id = "1",
                    Reference1 = new Entity2
                    {
                        Id = "1.1",
                        Collection = new List<Entity3>()
                        {
                            new Entity3 { Id = "1.1.1" },
                            new Entity3 { Id = "1.1.2" }
                        }
                    },
                    Reference2 = new Entity4 { Id = "1.2" }
                };

                var entity2 = new Entity1 { Id = "2", Reference1 = null, Reference2 = new Entity4 { Id = "2.2" } };
                var entity3 = new Entity1 { Id = "3", Reference1 = null, Reference2 = null };

                ctx.Entities1.Add(entity1);
                ctx.Entities1.Add(entity2);
                ctx.Entities1.Add(entity3);
                ctx.SaveChanges();
            }

            using (var ctx = new Issue2595Context())
            {
                var query = ctx.Entities1.Select(e => new DTO1
                {
                    Id = e.Id,
                    Reference1 = new DTO2
                    {
                        Id = e.Reference1.Id,
                        Collection = e.Reference1.Collection.Select(c => new DTO3 { Id = c.Id })
                    },
                    Reference2 = new DTO4 {  Id = e.Reference2.Id }
                }).Select(r => new { r.Id, IsNull1 = r.Reference1 == null, r.Reference1, IsNull2 = r.Reference2 == null, r.Reference2 });

                QueryTestHelpers.VerifyDbQuery(query, expectedSql);
                var results = query.ToList();

                Assert.Equal(3, results.Count);
                Assert.True(results.Select(r => r.IsNull1).All(r => !r));
                Assert.True(results.Select(r => r.IsNull2).All(r => !r));
                Assert.True(results.Select(r => r.Reference1).All(r => r != null));
                Assert.True(results.Select(r => r.Reference2).All(r => r != null));
            }
        }