Bug #92987 Entity Framework crashing when using linq Any() method
Submitted: 29 Oct 2018 12:57 Modified: 3 Nov 2018 17:42
Reporter: Manuel Zulian Email Updates:
Status: Open Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:8.0.13 OS:Microsoft Windows
Assigned to: CPU Architecture:x86
Tags: ANY, EF, linq

[29 Oct 2018 12:57] Manuel Zulian
Description:
Starting from a basic console application, adding only mysql.data.entityframeworkcore package and entityframework.design (for scaffolding), calling the Any() method from linq on a table to check for existence of records throws this exception:

System.InvalidOperationException: 'No coercion operator is defined between types 'System.Int16' and 'System.Boolean'.'

		StackTrace	"   at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)\r\n   at System.Linq.Expressions.Expression.Convert(Expression expression, Type type, MethodInfo method)\r\n   at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.CreateGetValueExpression(Expression dataReaderExpression, Expression indexExpression, TypeMaterializationInfo materializationInfo, Boolean box)\r\n   at System.Linq.Enumerable.SelectIterator[TSource,TResult](IEnumerable`1 source, Func`3 selector)+MoveNext()\r\n   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)\r\n   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)\r\n   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)\r\n   at System.Dynamic.Utils.CollectionExtensions.ToReadOnly[T](IEnumerable`1 enumerable)\r\n   at System.Linq.Expressions.Expression.NewArrayInit(Type type, IEnumerable`1 initializers)\r\n   at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.CreateArrayInitializer(CacheKey cacheKey)\r\n   at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.<Create>b__10_0(CacheKey k)\r\n   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)\r\n   at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func`2 valueFactory)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext.NotifyReaderCreated(DbDataReader dataReader)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()\r\n   at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.GetResult[TResult](IEnumerable`1 valueBuffers, Boolean throwOnNullResult)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ResultEnumerable`1.GetEnumerator()\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()\r\n   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)\r\n   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_1`1.<CompileQueryCore>b__0(QueryContext qc)\r\n   at System.Linq.Queryable.Any[TSource](IQueryable`1 source)\r\n   at AnyBugTest.Program.Main(String[] args) in C:\\Users\\****\\source\\repos\\AnyBugTest\\AnyBugTest\\Program.cs:line 14"	string

(using the any() from Microsoft.EntityFrameworkCore.Internal looks fine but it seems to be deprecated)

Example code:

using System;
using System.Linq;
using AnyBugTest.Models.Ctx;

namespace AnyBugTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var ctx = new Ctx();
            if(ctx.table.Any())
                Console.WriteLine("test");
        }
    }
}

How to repeat:
See description.
[3 Nov 2018 17:40] Bradley Grainger
Are you trying to map a BIT(1) column to bool? If so, see my comment on bug #93028.
[3 Nov 2018 17:42] Manuel Zulian
No, it's TINYINT(1).
[13 Nov 2018 18:15] Oliver Hall
I'm hitting the same issue, though I'm not creating any code myself.  I'm using migrations to add ASP.Net Identity to a MySQL schema.

[System.InvalidOperationException: No coercion operator is defined between types 'System.Int16' and 'System.Boolean'.]

The MySQL datatype for the generated tables is BIT(1) for all boolean properties (none of which are nullable) and I'm calling UserManager.FindByEmailAsync(...) which in turn calls Linq.

I can't see where the Int16 is coming from at all.

Apologies if I'm missing something obvious, or there's an easy way to add user-defined coercion, but I'm pretty new to Entity Framework.  Unfortunately, this is blocking further progress for me.
[12 Dec 2018 15:30] Andrew Furniss
I am also experiencing this issue since upgrading to MySQL Server 8 from 5.7. I'm on Windows and using MySQL.Data.EntityFrameworkCore 8.0.13 nuget package within a .NET Core Web API application.
[2 Apr 8:30] Marcos Gomes
Hi, I've notice that on MySQLTypeMapper if we change the _bit mapping to something like this:
private readonly RelationalTypeMapping _bit = new MySQLNumberTypeMapping("bit", typeof(bool), DbType.Boolean);
Ir resolves the error.

O hope the dev team could look at that.
Thanks