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

Union of Anonymous Type having hardwired bool property throws exception #12623

Closed
avinash-phaniraj-readify opened this issue Jul 11, 2018 · 6 comments

Comments

@avinash-phaniraj-readify

Performing a Union of Anonymous Types,
having a hardwired bool property throws exception

[Workaround : making it an expression such as 1 == 1 or even calling an extension method that returns itself gets us around.]

Exception message: An exception occurred while reading a database value. The expected type was 'System.Boolean' but the actual value was of type 'System.Int32'.
Stack trace:
System.InvalidOperationException
  HResult=0x80131509
  Message=An exception occurred while reading a database value. The expected type was 'System.Boolean' but the actual value was of type 'System.Int32'.
  Source=Microsoft.EntityFrameworkCore
  StackTrace:
   at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.ThrowReadValueException[TValue](Exception exception, Object value, IPropertyBase property)
   at Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
   at Microsoft.EntityFrameworkCore.Storage.Internal.NoopExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.<UnionIterator>d__67`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at TestHostForCastException.Program.Main(String[] args) in C:\EFCore\EFCoreTestBed\Program.cs:line 26

Inner Exception 1:
InvalidCastException: Specified cast is not valid.

Steps to reproduce

https://github.com/avinash-phaniraj-readify/EFCoreTestBed.git
Please checkout branch - UnionError

var employees = context.Set<Employee>();
var qualified = employees.Select(s => new { s.Id, IsQualified = true });
var unQualified = employees.Select(s => new { s.Id, IsQualified = false });
qualified.Union(unQualified).ToList();

Further technical details

EF Core version: 2.1.1
Database Provider: EntityFrameworkCore.SqlServerCompact35 2.1.0.1
Operating system: Win 10
IDE: Visual Studio 2017 15.7.2

@divega divega added this to the 2.2.0 milestone Jul 11, 2018
@divega
Copy link
Contributor

divega commented Jul 11, 2018

@maumar to check if this is already fixed or something we need to fix.

@maumar
Copy link
Contributor

maumar commented Jul 11, 2018

We have fixed this for sql server in 2.1.3 patch, the issue is tracked here: #12280

@ErikEJ for potential fix in SqlCE. The extensibility point has been added along with the sql server fix - it should just be a matter of implementing ApplyExplicitCastToBoolInProjectionOptimization

@divega
Copy link
Contributor

divega commented Jul 22, 2018

@maumar can you remind me if this is an issue that may need to be fixed in all providers or is it only in SQL CE?

@ErikEJ
Copy link
Contributor

ErikEJ commented Jul 22, 2018

Also SQL Server it looks like

@maumar
Copy link
Contributor

maumar commented Jul 24, 2018

@divega SqlServer and SqlCE. Sqlite is fine as is, but I'm not sure about postgres.

@roji
Copy link
Member

roji commented Jul 25, 2018

Thanks for thinking about me :)

If I understand this issue correctly, Npgsql/PostgreSQL should be fine because boolean literals are strongly-typed, recognized as such and don't need any casting. Running Correlated_collection_with_complex_order_by_funcletized_to_constant_bool on the current 2.2 branch (where I see the commit has been merged), I get the following SQL on PostgreSQL:

SELECT t.c, t."Nickname", t."SquadId", t."FullName", "g.Weapons"."Name", "g.Weapons"."OwnerFullName"
        FROM "Weapons" AS "g.Weapons"
        INNER JOIN (
            SELECT FALSE AS c, g0."Nickname", g0."SquadId", g0."FullName"
            FROM "Gears" AS g0
            WHERE g0."Discriminator" IN ('Officer', 'Gear')
        ) AS t ON "g.Weapons"."OwnerFullName" = t."FullName"
        ORDER BY t.c DESC NULLS LAST, t."Nickname" NULLS FIRST, t."SquadId" NULLS FIRST, t."FullName" NULLS FIRST

And SELECT FALSE is just fine.

Let me know if you think I need to test or do anything else.

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

6 participants