From 31eed909d47380812d94dddfc7c55d7dc35fb97f Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Fri, 27 Sep 2019 18:57:48 +0200 Subject: [PATCH] Add regression test Closes #13517 --- .../Query/QueryBugsTest.cs | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs index 6c44d1d5a89..648c6afea0a 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs @@ -6942,6 +6942,82 @@ private class DbGame #endregion + #region Issue13517 + + [Fact] + public void Query_filter_with_pk_fk_optimization_bug_13517() + { + using var _ = CreateDatabase13517(); + using var context = new BugContext13517(_options); + + context.Entities.Select(s => + new BugEntityDto13517 + { + Id = s.Id, + RefEntity = s.RefEntity == null + ? null + : new BugRefEntityDto13517 { Id = s.RefEntity.Id, Public = s.RefEntity.Public }, + RefEntityId = s.RefEntityId + }).Single(p => p.Id == 1); + } + + private SqlServerTestStore CreateDatabase13517() + => CreateTestStore( + () => new BugContext13517(_options), + context => + { + var refEntity = new BugRefEntity13517 { Public = false }; + context.RefEntities.Add(refEntity); + context.Entities.Add(new BugEntity13517 { RefEntity = refEntity }); + context.SaveChanges(); + + ClearLog(); + }); + + public class BugEntity13517 + { + public int Id { get; set; } + public int? RefEntityId { get; set; } + public BugRefEntity13517 RefEntity { get; set; } + } + + public class BugRefEntity13517 + { + public int Id { get; set; } + public bool Public { get; set; } + } + + public class BugEntityDto13517 + { + public int Id { get; set; } + public int? RefEntityId { get; set; } + public BugRefEntityDto13517 RefEntity { get; set; } + } + + public class BugRefEntityDto13517 + { + public int Id { get; set; } + public bool Public { get; set; } + } + + private class BugContext13517 : DbContext + { + public DbSet Entities { get; set; } + public DbSet RefEntities { get; set; } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().HasQueryFilter(f => f.Public == true); + } + + public BugContext13517(DbContextOptions options) + : base(options) + { + } + } + + #endregion + private DbContextOptions _options; private SqlServerTestStore CreateTestStore(