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

SqlServer-2.1 - InvalidCastException (Int32 to Boolean) #12280

Closed
huancz opened this issue Jun 7, 2018 · 6 comments
Closed

SqlServer-2.1 - InvalidCastException (Int32 to Boolean) #12280

huancz opened this issue Jun 7, 2018 · 6 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Milestone

Comments

@huancz
Copy link

huancz commented Jun 7, 2018

Simple .Select projection throws in Microsoft.EntityFrameworkCore.SqlServer 2.1 (worked fine in 2.0 or Memory DB of any version)

System.InvalidOperationException: An exception occurred while reading a database value. The expected type was 'System.Boolean' but the actual value was of type 'System.Int32'. ---> System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Boolean'.
         at System.Data.SqlClient.SqlBuffer.get_Boolean()
         at lambda_method(Closure , DbDataReader )
         --- End of inner exception stack trace ---
         at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.ThrowReadValueException[TValue](Exception excepti
on, Object value, IPropertyBase property)
         at lambda_method(Closure , DbDataReader )
         at Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
         at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
         at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
         at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
         at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptorUnhandled Exception:.MoveNext()

Steps to reproduce

Full repro as attachment, core of the issue is this snippet:

List<string> selectedNames = new List<string>();
var selectedList = context.Set<Entity>()
    .Select(x => new
    {
        Name = x.Name,
        Selected = selectedNames != null && selectedNames.Contains(x.Name)
    })
    .ToList()

It is specific to .Contains on empty list. Various workarounds:

  • selectedNames = null
  • selectedNames.Add("nonsense")
  • rewriting the projection to read "Selected = selectedNames != null && selectedNames.Any() && selectedNames.Contains(x.Name)"

This seems like repeat of issue #2246, but that one was fixed two years ago, now it's back again in 2.1. I couldn't find anything similar in currently open issues.

Further technical details

EF Core version: 2.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.7.3

repro-invalid-cast.zip

@ajcvickers
Copy link
Member

Possible duplicate of #12175 since they both involve empty Contains. /cc @maumar @smitpatel

@smitpatel
Copy link
Contributor

Duplicate of #12175

@smitpatel
Copy link
Contributor

Verified fixed in nightlies

@ajcvickers ajcvickers added this to the 2.1.3 milestone Jun 8, 2018
@ajcvickers ajcvickers added Servicing-consider closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. patch-approved and removed Servicing-consider labels Jun 18, 2018
@ajcvickers
Copy link
Member

This is approved for 2.1.3. Do not merge yet; branch is expected to open Monday.

Given these issues have been parked for some time, please be careful to ensure the correct commits get into the correct branches.

@ajcvickers
Copy link
Member

ajcvickers commented Jun 27, 2018

@maumar This issue is approved for patch and the release\2.1 branch is now open for merging. Please ensure:

  • The appropriate changes get into both the release and dev branches
  • Quirking is included for the release branch only

@maumar
Copy link
Contributor

maumar commented Jun 29, 2018

Merged to 2.1 in 77512c4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Projects
None yet
Development

No branches or pull requests

5 participants