in EFCore/src/Storage/Internal/MySQLTypeMappingSource.cs [228:321]
protected override RelationalTypeMapping? FindMapping(in RelationalTypeMappingInfo mappingInfo)
=> base.FindMapping(mappingInfo) ??
FindRawMapping(mappingInfo)?.Clone(mappingInfo);
private RelationalTypeMapping? FindRawMapping(RelationalTypeMappingInfo mappingInfo)
{
if (!_initialized)
{
Initialize();
_initialized = true;
}
var clrType = mappingInfo.ClrType;
var storeTypeName = mappingInfo.StoreTypeName;
var storeTypeNameBase = mappingInfo.StoreTypeNameBase;
if (storeTypeName != null)
{
if (_storeTypeMappings!.TryGetValue(storeTypeName, out var mapping))
{
return clrType == null
? mapping[0]
: mapping.FirstOrDefault(m => m.ClrType == clrType);
}
if (_storeTypeMappings.TryGetValue(storeTypeNameBase!, out mapping))
{
#if !NET8_0_OR_GREATER
return clrType == null
? mapping[0].Clone(in mappingInfo)
: mapping.FirstOrDefault(m => m.ClrType == clrType)?.Clone(in mappingInfo);
#else
return clrType == null
? mapping[0].WithTypeMappingInfo(mappingInfo)
: mapping.FirstOrDefault(m => m.ClrType == clrType)?.WithTypeMappingInfo(mappingInfo);
#endif
}
}
if (clrType != null)
{
if (_clrTypeMappings!.TryGetValue(clrType, out var mapping))
{
if (mappingInfo.Precision.HasValue)
{
if (clrType == typeof(DateTime) ||
clrType == typeof(DateTimeOffset) ||
clrType == typeof(TimeSpan))
{
#if !NET8_0_OR_GREATER
return mapping.Clone(mappingInfo.Precision.Value, null);
#else
return mapping.WithPrecisionAndScale(mappingInfo.Precision.Value, null);
#endif
}
}
return mapping;
}
else if (clrType == typeof(string))
{
var isAnsi = mappingInfo.IsUnicode == false;
var isFixedLength = mappingInfo.IsFixedLength == true;
var size = mappingInfo.Size ?? (mappingInfo.IsKeyOrIndex ?
Math.Min(MAXKEYLENGTH / (_options.CharSet!.byteCount * 2), 255)
: null);
if (size > 65_553 / _options.CharSet!.byteCount)
{
size = null;
isFixedLength = false;
}
mapping = isFixedLength ? _charUnicode : size == null
? _longtextUnicode : _varcharUnicode;
#if !NET8_0_OR_GREATER
return size == null ? mapping : mapping.Clone($"{mapping.StoreTypeNameBase}({size})", size);
#else
return size == null ? mapping : mapping.WithStoreTypeAndSize($"{mapping.StoreTypeNameBase}({size})", size);
#endif
}
else if (clrType == typeof(byte[]))
{
bool isFixedLength = mappingInfo.IsFixedLength == true;
var size = mappingInfo.Size ?? (mappingInfo.IsKeyOrIndex ? MAXKEYLENGTH : null);
return new MySQLByteArrayTypeMapping(
size: size,
fixedLength: isFixedLength);
}
}
return null;
}