efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Query: Remove ClientEval warning for Single()

Open LeandroPT opened this issue 10 years ago • 10 comments

Steps to reproduce

public class SecurityUser
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Required(AllowEmptyStrings = false)]
        [MaxLength(128)]
        public string Username { get; set; }
    }

SecurityUser securityUser = ctx.SecurityUsers.Where(su => su.Id.Equals(pId))
                                            .Single();

The issue

SQL Server Query generated: exec sp_executesql N'SELECT TOP(2) [u].[Id], [u].[Comments], [u].[CreatedBy], [u].[CreatedById], [u].[CreatedDate], [u].[DefaultStoreId], [u].[DisplayName], [u].[EndDate], [u].[Language], [u].[LastModifiedBy], [u].[LastModifiedById], [u].[LastModifiedDate], [u].[StartDate], [u].[Status], [u].[Type], [u].[Username] FROM [SecurityUsers] AS [u] WHERE [u].[Id] = @__User_Id_0',N'@__User_Id_0 int',@__User_Id_0=1

EF Warning: warn: Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryCompilationContextFactory[8] The LINQ expression 'Single()' could not be translated and will be evaluated locally. To configure this warning use the DbContextOptionsBuilder.ConfigureWarnings API (event id 'RelationalEventId.QueryClientEvaluationWarning'). ConfigureWarnings can be used when overriding the DbContext.OnConfiguring method or using AddDbContext on the application service provider.

Further technical details

dotnet --version 1.0.0-preview2-003121

dotnet Microsoft .NET Core Shared Framework Host Version : 1.0.1 Build : cee57bf6c981237d80aa1631cfe83cb9ba329f12

EF Version: "Microsoft.EntityFrameworkCore": "1.0.0", "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0", Operating system: Win 10 X64 Visual Studio version: (2015 Update 3)

LeandroPT avatar Jun 29 '16 12:06 LeandroPT

I too am seeing the warning about LINQ expressions being evaluated locally. I'm not sure if this is a bug or a current limitation of Entity Framework Core.

SELECT TOP (2) is not a bug. Single() and SingleOrDefault() are designed for queries that the developer knows should only return one result. A return of more than one result causes an InvalidOperationException to be thrown alerting the developer that the 'Sequence contains more than one element'. This exception is thrown in code, so the only way to determine if more than one record exists is to return TOP 2 instead of TOP 1 and then count the resulting records.

ketrex2 avatar Jun 30 '16 15:06 ketrex2

This appears to be a false warning, changing the title to reflect that.

As @Ketrex mentioned, we use TOP 2 to ensure there is in fact only a single row when using Single(). If you want a TOP 1 query, you can use First() - but of course that won't throw if there are multiple rows.

rowanmiller avatar Jun 30 '16 17:06 rowanmiller

Hey,

@rowanmiller , yeah, but i think a top(2) that on a primary key field seems "dumb". i can see a lot of DBA's asking why...

Current SQL Server versions executes the query correctly, chooses the index wisely and ignores top2. Hope it keeps that way on future releases.

LeandroPT avatar Jun 30 '16 17:06 LeandroPT

@LeandroPT If you are sure there will only be a single element, then you can use First instead of Single.

anpete avatar Jun 30 '16 18:06 anpete

@anpete, No, it's cool... I understood the 1st time... The thing is i see DBA's calling us idiots :) Top(2) or TOP(1) in an PK column will give the same smirk :) (those guys can't do queries)...

LeandroPT avatar Jun 30 '16 18:06 LeandroPT

@LeandroPT There's no downside to using Top 2 on PK (or any other Unique index). And even if it's not a unique index, but a regular index, the difference is extremely small.

Pragmatic and experienced DBAs are OK with LINQ queries like these.

gdoron avatar Jun 30 '16 21:06 gdoron

It is crucial to fix this ASAP because we have to disable the throw on client evaluation.

gdoron avatar Jul 01 '16 07:07 gdoron

I still see no fix almost 5 months latter.. I am getting this error in "Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryCompilationContextFactory[", in other words inside microsoft relaeaseed code that throws about 50 paragraphs of useless stack traces because nothing ever get out of the Microsoft code - never hits user code.

The title of this thread is "Remove ClientEval warning for Single()", so how to do that should be whats in the thread!

pgorbas avatar Jan 25 '17 21:01 pgorbas

@pgorbas in case it isn't clear this issue was a bout a false positive for the warning which was fixed in version 1.1.0. If you are using 1.1.0 and you think you are running into the same problem or a similar one I would recommend that you create a new GitHub issue and include a repro.

divega avatar Jan 25 '17 21:01 divega

Any chance of including this as a hotfix for version 1.0 ?

dazinator avatar Jun 09 '17 14:06 dazinator