Efcore: Implicit operators in query for nullable FKs

Created on 19 Dec 2018  路  6Comments  路  Source: dotnet/efcore

Hi,
My issue is somehow similar to
https://github.com/aspnet/EntityFrameworkCore/issues/13669

I have nullable FK and created helper class to seamlessly manage nullable and non-nullable queries (see Ref class)

Problem is that implicit operators are not translated into remote SQL query and the whole dataset is fetched and then processed locally.

I created test project here
https://github.com/Evgeny-at-avocado/EFImplicitOperator

Locally generated query is fine (with some code that traverse EF internal), but actual query is different

                var query = ctx.Parents.Where(p => p.OptionalChildId == childRef);
                var localSql = query.ToSql();
                /* Local sql
                SELECT [p].[Id], [p].[OptionalChildId], [p].[RequiredChildId]
                FROM [Parents] AS [p]
                WHERE [p].[OptionalChildId] = '52a459e2-6d9e-4bf2-a744-1be80a9afef9'
                */

                var parent = query.Single();

                /* Query, captured by SQL Profiler
                SELECT [p].[Id], [p].[OptionalChildId], [p].[RequiredChildId]
                FROM [Parents] AS [p]
                */

NetCore 2.2
EF Core 2.2

Thanks,
Regards,
Evgeny

closed-fixed customer-reported type-bug

Most helpful comment

Fixed by #13549

Issue here was the parameter we generated was of Type of Ref and being converted to Guid in expression tree using convert node. Hence we caused client evaluation.

All 6 comments

Generated query in SQL profiler in EF Core 3.0

exec sp_executesql N'SELECT TOP(2) [p].[Id], [p].[OptionalChildId], [p].[RequiredChildId]
FROM [Parents] AS [p]
WHERE [p].[OptionalChildId] = @__p_0',N'@__p_0 uniqueidentifier',@__p_0='52A459E2-6D9E-4BF2-A744-1BE80A9AFEF9'

Fixed by #13549

Issue here was the parameter we generated was of Type of Ref and being converted to Guid in expression tree using convert node. Hence we caused client evaluation.

The ToSql method here is misleading. ToSql method does not account for parameters hence it compiled out the whole closure variable to guid constant and hence it was able to translate.

This and other bugfixes are scheduled for release with EF core 3.0, which is currently estimated to be ready "in 2019". Or is there more current information about the schedule? Will there be a backport of some bugfixes, such that there is a minor release before 3.0?

@HermannGruber I can't share anything more on the current schedule at this time. There is no minor release scheduled before 3.0. Certain high-impact issues than can be fixed with relatively low risk are fixed in patch releases, which usually ship approximately monthly. For example, 2.2.1 has shipped, 2.2.2 is being worked on.

If you believe this issue is something we should patch, then we can discuss, but it doesn't immediately seem like a high-impact issue. Can you make a case why we should reconsider?

@ajcvickers thanks for the feedback, and your offer. Basically, this was what I wanted to know - in general. Currently I am not really blocked by this issue, we can work around this in the meantime.

Was this page helpful?
0 / 5 - 0 ratings