Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We don't know how to bind property to OPENJSON subquery in some scenarios #33215

Open
maumar opened this issue Mar 1, 2024 · 0 comments
Open
Assignees
Milestone

Comments

@maumar
Copy link
Contributor

maumar commented Mar 1, 2024

For future follow up:

            var query2 = (from b1 in ctx.Blogs
                          from b2 in ctx.Blogs
                          where b1.Posts.Contains(b2.Posts.FirstOrDefault())
                          select new { b1, b2 }).ToList();

    public class BlogContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Repro;Trusted_Connection=True;MultipleActiveResultSets=true");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Blog>().OwnsMany(b => b.Posts, b => { b.ToJson(); });
        }
    }

    public class Blog
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int Foo { get; set; }
        public string Title { get; set; }
    }

throws:

System.Collections.Generic.KeyNotFoundException : The given key 'EmptyProjectionMember' was not present in the dictionary.

  Stack Trace: 
Dictionary`2.get_Item(TKey key)
SelectExpression.GetProjection(ProjectionBindingExpression projectionBindingExpression) line 1414
RelationalSqlTranslatingExpressionVisitor.BindProperty(StructuralTypeReferenceExpression typeReference, IProperty property) line 1367
RelationalSqlTranslatingExpressionVisitor.TryBindMember(Expression source, MemberIdentity member, Expression& expression, IPropertyBase& property) line 1257
RelationalSqlTranslatingExpressionVisitor.TryBindMember(Expression source, MemberIdentity member, Expression& expression) line 1227
RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 769
SqlServerSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 202
RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 840
SqlServerSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) line 202
RelationalSqlTranslatingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression) line 422
<35 more frames...>

We are trying to rewrite this to entity equlity. Source we try to bind is:

SELECT TOP(1) p1.Foo
FROM OPENJSON(b0.Posts, '') WITH (Foo int 'Foo', Title nvarchar(max) 'Title') AS p1
ORDER BY CAST(p1.key AS int) ASC

and the property is Title.

This is likely invalid scenario (JSON entity comparison), but there might be similar cases like this that are valid, but still fail currently - need to investigate further

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants