{"action":"closed","issue":{"url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/12256","repository_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore","labels_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/12256/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/12256/comments","events_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/12256/events","html_url":"https://github.com/aspnet/EntityFrameworkCore/issues/12256","id":329831097,"node_id":"MDU6SXNzdWUzMjk4MzEwOTc=","number":12256,"title":"Null Reference when using AS in ternary in EF Projection","user":{"login":"EddyPPP","id":11228834,"node_id":"MDQ6VXNlcjExMjI4ODM0","avatar_url":"https://avatars0.githubusercontent.com/u/11228834?v=4","gravatar_id":"","url":"https://api.github.com/users/EddyPPP","html_url":"https://github.com/EddyPPP","followers_url":"https://api.github.com/users/EddyPPP/followers","following_url":"https://api.github.com/users/EddyPPP/following{/other_user}","gists_url":"https://api.github.com/users/EddyPPP/gists{/gist_id}","starred_url":"https://api.github.com/users/EddyPPP/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/EddyPPP/subscriptions","organizations_url":"https://api.github.com/users/EddyPPP/orgs","repos_url":"https://api.github.com/users/EddyPPP/repos","events_url":"https://api.github.com/users/EddyPPP/events{/privacy}","received_events_url":"https://api.github.com/users/EddyPPP/received_events","type":"User","site_admin":false},"labels":[{"id":403965595,"node_id":"MDU6TGFiZWw0MDM5NjU1OTU=","url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/labels/closed-question","name":"closed-question","color":"777777","default":false},{"id":936619505,"node_id":"MDU6TGFiZWw5MzY2MTk1MDU=","url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/labels/customer-reported","name":"customer-reported","color":"a4a5f9","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":9,"created_at":"2018-06-06T11:40:53Z","updated_at":"2018-06-27T20:47:57Z","closed_at":"2018-06-27T20:47:57Z","author_association":"NONE","body":"I have a derived class with a nullable reference property.\r\nMy DbContext surfaces a DbSet of a base class.\r\nIf I try to optionally cast it in a ternary, I get the following error:\r\n\r\n```\r\nException message:\r\nObject reference not set to an instance of an object.\r\nStack trace:\r\n   at Microsoft.EntityFrameworkCore.Extensions.Internal.EFPropertyExtensions.CreateEFPropertyExpression(Expression target, Type propertyDeclaringType, Type propertyType, String propertyName, Boolean makeNullable)\r\n   at Microsoft.EntityFrameworkCore.Extensions.Internal.EFPropertyExtensions.CreateEFPropertyExpression(Expression target, IPropertyBase property, Boolean makeNullable)\r\n   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.EntityEqualityRewritingExpressionVisitor.CreateKeyAccessExpression(Expression target, IReadOnlyList`1 properties, Boolean nullComparison)\r\n   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.EntityEqualityRewritingExpressionVisitor.RewriteNullEquality(ExpressionType nodeType, Expression nonNullExpression)\r\n   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.EntityEqualityRewritingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression)\r\n   at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitConditional(ConditionalExpression node)\r\n   at System.Linq.Expressions.ConditionalExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitConditional(ConditionalExpression node)\r\n   at System.Linq.Expressions.ConditionalExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at Remotion.Linq.Clauses.SelectClause.TransformExpressions(Func`2 transformation)\r\n   at Remotion.Linq.QueryModel.TransformExpressions(Func`2 transformation)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryOptimizer.Optimize(QueryCompilationContext queryCompilationContext, QueryModel queryModel)\r\n   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.OptimizeQueryModel(QueryModel queryModel, Boolean asyncQuery)\r\n   at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.OptimizeQueryModel(QueryModel queryModel, Boolean asyncQuery)\r\n   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)\r\n   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_0`1.<Execute>b__0()\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)\r\n   at Remotion.Linq.QueryableBase`1.GetEnumerator()\r\n   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at AsTernaryIssue.Program.Main(String[] args) in C:\\....\\AsTernaryIssue\\Program.cs:line 45\r\n```\r\n\r\n### Steps to reproduce\r\nSample project demonstrating the simplest example attached:\r\n[AsTernaryIssue.zip](https://github.com/aspnet/EntityFrameworkCore/files/2075953/AsTernaryIssue.zip)\r\n\r\nSummary is:\r\n\r\nDBContext:\r\n\r\n```c#\r\n public class MyDbContext : DbContext\r\n    {\r\n        public DbSet<BaseClass> Entities { get; set; }\r\n\r\n        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\r\n        {\r\n            optionsBuilder.UseSqlServer(\"Server=.\\\\SQLSERVER2017DEV; Database=Example; Trusted_Connection=true;\");\r\n        }\r\n\r\n        protected override void OnModelCreating(ModelBuilder modelBuilder)\r\n        {\r\n            base.OnModelCreating(modelBuilder);\r\n\r\n            modelBuilder.Entity<DerivedClass>();\r\n        }\r\n    }\r\n```\r\n\r\nDerived Model:\r\n\r\n```c#\r\npublic class DerivedClass : BaseClass\r\n    {\r\n        public Guid? ChildId { get; set; }\r\n        public virtual ChildClass Child { get; set; }\r\n    }\r\n```\r\n\r\nNow run this query:\r\n\r\n```c#\r\ndb.Entities.AsNoTracking()\r\n                    .Select(x =>\r\n                        x is DerivedClass ?\r\n                        (\r\n                            (x as DerivedClass).Child != null ?\r\n                            (x as DerivedClass).Child.Name :\r\n                            String.Empty\r\n                        ) :\r\n                        String.Empty\r\n                    )\r\n                    .ToList()\r\n```\r\n\r\n### Further technical details\r\nEF Core version: 2.1\r\nDatabase Provider: Microsoft.EntityFrameworkCore.SqlServer\r\nOperating system: Visual Studio 2017 15.7.3\r\n"}}
{"actor":{"display_login":"ajcvickers"}}