title: No coercion operator is defined between types 'System.Int16' and 'System.Boolean'
body: <p>I am using a helper class to build an expression which I then want to pass to a <code>.Select()</code> call on a EF Core <code>DbSet</code> so that I can dynamically select fields from the database and ignore large fields that I don't care about.</p>
  
  <p>The helper class looks like this:</p>
  
  <pre><code>public static class ExpressionHelper
  {
      public static Expression&lt;Func&lt;T, T&gt;&gt; BuildFieldSelector&lt;T&gt;(IEnumerable&lt;string&gt; fields)
      {
          // input parameter "x"
          var param = Expression.Parameter(typeof(T), "x");
  
          // new statement "new Data()"
          var newExpression = Expression.New(typeof(T));
  
          // create initializers
          var bindings = fields
              .Select(x =&gt; {
                  // property "Field1"
                  var property = typeof(T).GetProperty(x.Trim(), BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
  
                  if (property == null)
                      return null;
  
                  // original value "x.Field1"
                  var memberExpression = Expression.Property(param, property);
  
                  // set value "Field1 = x.Field1"
                  return Expression.Bind(property, memberExpression);
              })
              .Where(x =&gt; x != null);
  
          // initialization "new Data { Field1 = x.Field1, Field2 = x.Field2 }"
          var memberInit = Expression.MemberInit(newExpression, bindings);
  
          // expression "x =&gt; new Data { Field1 = x.Field1, Field2 = x.Field2 }"
          return Expression.Lambda&lt;Func&lt;T, T&gt;&gt;(memberInit, param);
      }
  }
  </code></pre>
  
  <p>As I say, the expression this produces gets passed to a <code>.Select()</code> LINQ call on a EF DbSet.</p>
  
  <p>This was all working fine but I'm now experiencing the following exception:</p>
  
  <pre><code>No coercion operator is defined between types 'System.Int16' and 'System.Boolean'.
     at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
     at System.Linq.Expressions.Expression.Convert(Expression expression, Type type, MethodInfo method)
     at System.Linq.Expressions.Expression.Convert(Expression expression, Type type)
     at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.CreateGetValueExpression(Expression dataReaderExpression, Expression indexExpression, TypeMaterializationInfo materializationInfo, Boolean box)
     at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.&lt;&gt;c.&lt;CreateArrayInitializer&gt;b__12_0(TypeMaterializationInfo mi, Int32 i)
     at System.Linq.Enumerable.SelectIterator[TSource,TResult](IEnumerable`1 source, Func`3 selector)+MoveNext()
     at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
     at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
     at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
     at System.Dynamic.Utils.CollectionExtensions.ToReadOnly[T](IEnumerable`1 enumerable)
     at System.Linq.Expressions.Expression.NewArrayInit(Type type, IEnumerable`1 initializers)
     at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.CreateArrayInitializer(CacheKey cacheKey)
     at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.&lt;Create&gt;b__10_0(CacheKey k)
     at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
     at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.Create(IReadOnlyList`1 types)
     at Microsoft.EntityFrameworkCore.Query.Sql.DefaultQuerySqlGenerator.CreateValueBufferFactory(IRelationalValueBufferFactoryFactory relationalValueBufferFactoryFactory, DbDataReader dataReader)
     at Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext.&lt;NotifyReaderCreated&gt;b__14_0(FactoryAndReader s)
     at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue&amp; target, TParam param, Func`2 valueFactory)
     at Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext.NotifyReaderCreated(DbDataReader dataReader)
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
     at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
     at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
     at TAP.Services.TourManagementService.GetReviews(String tourId, IEnumerable`1 fields) in C:\Projects\TheAdventurePeople\TAP.Services\TAP.Services\Services\TourManagementService.cs:line 322
  </code></pre>
  
  <p>I have not changed the code at all since it was working so I'm not really sure what's caused it. I've also not changed any types in the model class I'm using for the expression. The expression that's being build in this instance looks like this:</p>
  
  <pre><code>{x =&gt; new TourReview() {ReviewId = x.ReviewId, TourId = x.TourId, Score = x.Score, ReviewerName = x.ReviewerName, IsHidden = x.IsHidden}}
  </code></pre>
  
  <p>and the TourReview class is as follows:</p>
  
  <pre><code>public class TourReview
  {
      public int ReviewId { get; set; }
      [Required]
      public string TourId { get; set; }
      [Required]
      [DataType(DataType.Date)]
      [Display(Name = "Review Date")]
      public DateTime DateReviewed { get; set; }
      [Required]
      public decimal Score { get; set; }
      public string Title { get; set; }
      [Required]
      [Display(Name = "Review Text")]
      public string Text { get; set; }
      [Required]
      [Display(Name = "Reviewer Name")]
      public string ReviewerName { get; set; }
      public bool IsHidden { get; set; }
  }
  </code></pre>
  
  <p>I did recently upgrade to .NET Core 2.1 and to MySQL 8.0.13 from 5.7 if that's of any relevance.</p>
  
  <p><strong>EDIT:</strong>
  I've also just remembered that I've recently changed from the Pomelo MySQL connector to the official MySQL.Data.EntityFrameworkCore connector.</p>
  
  <p><strong>EDIT 2:</strong>
  I believe this started happening after I u[grade MySQL Server on my local machine. The new version seems to be causing problems with the type mapping. Adding <code>.Property(x =&gt; x.IsHidden).HasConversion(x =&gt; Convert.ToInt32(x), x =&gt; Convert.ToBoolean(x));</code> to the model builder resolves the problem but I don't want to have to do this for every boolean property I have!</p>