EF Core 3.0 的奇怪问题:Invalid cast from 'System.I

2019-10-16 17:01发布

问题:

同样的 LINQ

var result = await _postQueryRepository
    .GetPostsByStartId(blogId, startId, postType, postConfig, startDate)
    .OrderBy(p => p.Id)
    .Take(itemCount)
    .ProjectToType<T>()
    .ToListAsync();

当 itemCount > 1 时,能正常执行。
当 itemCount = 1 时,则报下面的异常:

System.InvalidCastException: Invalid cast from 'System.Int32' to 'PostConfig'.
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at System.Int32.System.IConvertible.ToType(Type type, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType)
   at Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter`2.Sanitize[T](Object value)
   at Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter`2.<>c__DisplayClass3_0`2.<SanitizeConverter>b__0(Object v)
   at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping.CreateParameter(DbCommand command, String name, Object value, Nullable`1 nullable)

请问如何解决?
注:PostConfig 是枚举类型。

回答1:

是 ProjectToType 对应的 Mapster 映射配置引起的

.Map(dest => dest.IsPrivated, src => (src.PostConfig & PostConfig.IsActive) == PostConfig.Empty)


回答2:

多半是数据库里的数据有问题