同样的 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 是枚举类型。
异常出现在 Created DbCommand for '"ExecuteReader"'
之后。
将 .ToListAsync()
改为 .FirstOrDefaultAsync()
则没这个问题。
是 ProjectToType 对应的 Mapster 映射配置引起的
.Map(dest => dest.IsPrivated, src => (src.PostConfig & PostConfig.IsActive) == PostConfig.Empty)
多半是数据库里的数据有问题