Skip to content

Commit 3a71601

Browse files
author
msftbot[bot]
authored
Merge pull request #31632 from dotnet-maestro-bot/merge/release/8.0-to-main
[automated] Merge branch 'release/8.0' => 'main'
2 parents 461eff6 + d38b60a commit 3a71601

File tree

14 files changed

+370
-163
lines changed

14 files changed

+370
-163
lines changed

src/EFCore.Cosmos/Storage/Internal/CosmosTypeMappingSource.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public CosmosTypeMappingSource(TypeMappingSourceDependencies dependencies)
7171
private CoreTypeMapping? FindCollectionMapping(in TypeMappingInfo mappingInfo)
7272
{
7373
var clrType = mappingInfo.ClrType!;
74+
75+
if (mappingInfo.ElementTypeMapping != null)
76+
{
77+
return null;
78+
}
79+
7480
var elementType = clrType.TryGetSequenceType();
7581
if (elementType == null)
7682
{

src/EFCore.InMemory/Storage/Internal/InMemoryTypeMappingSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public InMemoryTypeMappingSource(TypeMappingSourceDependencies dependencies)
3737

3838
if (clrType.IsValueType
3939
|| clrType == typeof(string)
40-
|| clrType == typeof(byte[]))
40+
|| (clrType == typeof(byte[]) && mappingInfo.ElementTypeMapping == null))
4141
{
4242
return new InMemoryTypeMapping(
4343
clrType, jsonValueReaderWriter: jsonValueReaderWriter);

src/EFCore.Relational/Storage/RelationalTypeMappingInfo.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public RelationalTypeMappingInfo(
151151
int? scale)
152152
{
153153
// Note: Empty string is allowed for store type name because SQLite
154-
_coreTypeMappingInfo = new TypeMappingInfo(null, false, unicode, size, null, precision, scale);
154+
_coreTypeMappingInfo = new TypeMappingInfo(null, null, false, unicode, size, null, precision, scale);
155155
StoreTypeName = storeTypeName;
156156
StoreTypeNameBase = storeTypeNameBase;
157157
IsFixedLength = null;
@@ -161,6 +161,7 @@ public RelationalTypeMappingInfo(
161161
/// Creates a new instance of <see cref="RelationalTypeMappingInfo" />.
162162
/// </summary>
163163
/// <param name="member">The property or field for which mapping is needed.</param>
164+
/// <param name="elementTypeMapping">The type mapping for elements, if known.</param>
164165
/// <param name="storeTypeName">The provider-specific relational type name for which mapping is needed.</param>
165166
/// <param name="storeTypeNameBase">The provider-specific relational type name, with any facets removed.</param>
166167
/// <param name="unicode">Specifies Unicode or ANSI mapping, or <see langword="null" /> for default.</param>
@@ -169,14 +170,15 @@ public RelationalTypeMappingInfo(
169170
/// <param name="scale">Specifies a scale for the mapping, or <see langword="null" /> for default.</param>
170171
public RelationalTypeMappingInfo(
171172
MemberInfo member,
173+
RelationalTypeMapping? elementTypeMapping = null,
172174
string? storeTypeName = null,
173175
string? storeTypeNameBase = null,
174176
bool? unicode = null,
175177
int? size = null,
176178
int? precision = null,
177179
int? scale = null)
178180
{
179-
_coreTypeMappingInfo = new TypeMappingInfo(member, unicode, size, precision, scale);
181+
_coreTypeMappingInfo = new TypeMappingInfo(member, elementTypeMapping, unicode, size, precision, scale);
180182

181183
StoreTypeName = storeTypeName;
182184
StoreTypeNameBase = storeTypeNameBase;
@@ -212,6 +214,7 @@ public RelationalTypeMappingInfo(
212214
/// Creates a new instance of <see cref="TypeMappingInfo" />.
213215
/// </summary>
214216
/// <param name="type">The CLR type in the model for which mapping is needed.</param>
217+
/// <param name="elementTypeMapping">The type mapping for elements, if known.</param>
215218
/// <param name="storeTypeName">The database type name.</param>
216219
/// <param name="storeTypeNameBase">The provider-specific relational type name, with any facets removed.</param>
217220
/// <param name="keyOrIndex">If <see langword="true" />, then a special mapping for a key or index may be returned.</param>
@@ -224,6 +227,7 @@ public RelationalTypeMappingInfo(
224227
/// <param name="dbType">The suggested <see cref="DbType"/>, or <see langword="null" /> for default.</param>
225228
public RelationalTypeMappingInfo(
226229
Type? type = null,
230+
RelationalTypeMapping? elementTypeMapping = null,
227231
string? storeTypeName = null,
228232
string? storeTypeNameBase = null,
229233
bool keyOrIndex = false,
@@ -235,7 +239,7 @@ public RelationalTypeMappingInfo(
235239
int? scale = null,
236240
DbType? dbType = null)
237241
{
238-
_coreTypeMappingInfo = new TypeMappingInfo(type, keyOrIndex, unicode, size, rowVersion, precision, scale);
242+
_coreTypeMappingInfo = new TypeMappingInfo(type, elementTypeMapping, keyOrIndex, unicode, size, rowVersion, precision, scale);
239243

240244
IsFixedLength = fixedLength;
241245
StoreTypeName = storeTypeName;
@@ -341,6 +345,15 @@ public JsonValueReaderWriter? JsonValueReaderWriter
341345
init => _coreTypeMappingInfo = _coreTypeMappingInfo with { JsonValueReaderWriter = value };
342346
}
343347

348+
/// <summary>
349+
/// The element type of the mapping, if any.
350+
/// </summary>
351+
public RelationalTypeMapping? ElementTypeMapping
352+
{
353+
get => (RelationalTypeMapping?)_coreTypeMappingInfo.ElementTypeMapping;
354+
init => _coreTypeMappingInfo = _coreTypeMappingInfo with { ElementTypeMapping = value };
355+
}
356+
344357
/// <summary>
345358
/// Returns a new <see cref="RelationalTypeMappingInfo" /> with the given converter applied.
346359
/// </summary>

src/EFCore.Relational/Storage/RelationalTypeMappingSource.cs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
8585
RelationalStrings.NoneRelationalTypeMappingOnARelationalTypeMappingSource);
8686

8787
private RelationalTypeMapping? FindMappingWithConversion(
88-
in RelationalTypeMappingInfo mappingInfo,
88+
RelationalTypeMappingInfo mappingInfo,
8989
IReadOnlyList<IProperty>? principals)
9090
{
9191
Type? providerClrType = null;
@@ -114,15 +114,19 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
114114
}
115115
}
116116

117-
var element = principal.GetElementType();
118-
if (element != null)
117+
if (elementMapping == null)
119118
{
120-
elementMapping = FindMapping(element);
119+
var element = principal.GetElementType();
120+
if (element != null)
121+
{
122+
elementMapping = FindMapping(element);
123+
mappingInfo = mappingInfo with { ElementTypeMapping = (RelationalTypeMapping?)elementMapping };
124+
}
121125
}
122126
}
123127
}
124128

125-
var resolvedMapping = FindMappingWithConversion(mappingInfo, providerClrType, customConverter, elementMapping);
129+
var resolvedMapping = FindMappingWithConversion(mappingInfo, providerClrType, customConverter);
126130

127131
ValidateMapping(resolvedMapping, principals?[0]);
128132

@@ -132,26 +136,23 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
132136
private RelationalTypeMapping? FindMappingWithConversion(
133137
RelationalTypeMappingInfo mappingInfo,
134138
Type? providerClrType,
135-
ValueConverter? customConverter,
136-
CoreTypeMapping? elementMapping)
139+
ValueConverter? customConverter)
137140
=> _explicitMappings.GetOrAdd(
138-
(mappingInfo, providerClrType, customConverter, elementMapping),
141+
(mappingInfo, providerClrType, customConverter, mappingInfo.ElementTypeMapping),
139142
static (k, self) =>
140143
{
141144
var (mappingInfo, providerClrType, customConverter, elementMapping) = k;
142145

143146
var sourceType = mappingInfo.ClrType;
144-
RelationalTypeMapping? mapping = null;
147+
var mapping = providerClrType == null
148+
|| providerClrType == mappingInfo.ClrType
149+
? self.FindMapping(mappingInfo)
150+
: null;
145151

146-
if (elementMapping == null
147-
|| customConverter != null)
152+
if (mapping == null)
148153
{
149-
mapping = providerClrType == null
150-
|| providerClrType == mappingInfo.ClrType
151-
? self.FindMapping(mappingInfo)
152-
: null;
153-
154-
if (mapping == null)
154+
if (elementMapping == null
155+
|| customConverter != null)
155156
{
156157
if (sourceType != null)
157158
{
@@ -193,10 +194,10 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
193194
mapping ??= self.FindCollectionMapping(mappingInfo, sourceType, providerClrType, elementMapping);
194195
}
195196
}
196-
}
197-
else if (sourceType != null)
198-
{
199-
mapping = self.FindCollectionMapping(mappingInfo, sourceType, providerClrType, elementMapping);
197+
else if (sourceType != null)
198+
{
199+
mapping = self.FindCollectionMapping(mappingInfo, sourceType, providerClrType, elementMapping);
200+
}
200201
}
201202

202203
if (mapping != null
@@ -312,7 +313,7 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
312313

313314
var resolvedMapping = FindMappingWithConversion(
314315
new RelationalTypeMappingInfo(elementType, storeTypeName, storeTypeNameBase, unicode, isFixedLength, size, precision, scale),
315-
providerClrType, customConverter, null);
316+
providerClrType, customConverter);
316317

317318
ValidateMapping(resolvedMapping, null);
318319

@@ -357,7 +358,7 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
357358
ValueConverter? customConverter = null;
358359
if (typeConfiguration == null)
359360
{
360-
mappingInfo = new RelationalTypeMappingInfo(type);
361+
mappingInfo = new RelationalTypeMappingInfo(type, (RelationalTypeMapping?)elementMapping);
361362
}
362363
else
363364
{
@@ -379,6 +380,7 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
379380
var isFixedLength = (bool?)typeConfiguration[RelationalAnnotationNames.IsFixedLength];
380381
mappingInfo = new RelationalTypeMappingInfo(
381382
customConverter?.ProviderClrType ?? type,
383+
(RelationalTypeMapping?)elementMapping,
382384
storeTypeName,
383385
storeTypeBaseName,
384386
keyOrIndex: false,
@@ -390,7 +392,7 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
390392
scale: scale);
391393
}
392394

393-
return FindMappingWithConversion(mappingInfo, providerClrType, customConverter, (RelationalTypeMapping?)elementMapping);
395+
return FindMappingWithConversion(mappingInfo, providerClrType, customConverter);
394396
}
395397

396398
/// <summary>
@@ -422,7 +424,7 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
422424
attribute.TypeName, ref unicode, ref size, ref precision, ref scale);
423425

424426
return FindMappingWithConversion(
425-
new RelationalTypeMappingInfo(member, storeTypeName, storeTypeNameBase, unicode, size, precision, scale), null);
427+
new RelationalTypeMappingInfo(member, null, storeTypeName, storeTypeNameBase, unicode, size, precision, scale), null);
426428
}
427429

428430
return FindMappingWithConversion(new RelationalTypeMappingInfo(member), null);
@@ -496,7 +498,7 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
496498

497499
return FindMappingWithConversion(
498500
new RelationalTypeMappingInfo(
499-
type, storeTypeName, storeTypeBaseName, keyOrIndex, unicode, size, rowVersion, fixedLength, precision, scale), null);
501+
type, null, storeTypeName, storeTypeBaseName, keyOrIndex, unicode, size, rowVersion, fixedLength, precision, scale), null);
500502
}
501503

502504
/// <inheritdoc />

src/EFCore.SqlServer/Storage/Internal/SqlServerTypeMappingSource.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -349,20 +349,23 @@ public SqlServerTypeMappingSource(
349349
return Rowversion;
350350
}
351351

352-
var isFixedLength = mappingInfo.IsFixedLength == true;
353-
354-
var size = mappingInfo.Size ?? (mappingInfo.IsKeyOrIndex ? 900 : null);
355-
if (size is < 0 or > 8000)
352+
if (mappingInfo.ElementTypeMapping == null)
356353
{
357-
size = isFixedLength ? 8000 : null;
358-
}
354+
var isFixedLength = mappingInfo.IsFixedLength == true;
359355

360-
return size == null
361-
? VariableLengthMaxBinary
362-
: new SqlServerByteArrayTypeMapping(
363-
size: size,
364-
fixedLength: isFixedLength,
365-
storeTypePostfix: storeTypeName == null ? StoreTypePostfix.Size : StoreTypePostfix.None);
356+
var size = mappingInfo.Size ?? (mappingInfo.IsKeyOrIndex ? 900 : null);
357+
if (size is < 0 or > 8000)
358+
{
359+
size = isFixedLength ? 8000 : null;
360+
}
361+
362+
return size == null
363+
? VariableLengthMaxBinary
364+
: new SqlServerByteArrayTypeMapping(
365+
size: size,
366+
fixedLength: isFixedLength,
367+
storeTypePostfix: storeTypeName == null ? StoreTypePostfix.Size : StoreTypePostfix.None);
368+
}
366369
}
367370
}
368371

src/EFCore.Sqlite.Core/Storage/Internal/SqliteTypeMappingSource.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ public static bool IsSpatialiteType(string columnType)
136136
private RelationalTypeMapping? FindRawMapping(RelationalTypeMappingInfo mappingInfo)
137137
{
138138
var clrType = mappingInfo.ClrType;
139+
if (clrType == typeof(byte[]) && mappingInfo.ElementTypeMapping != null)
140+
{
141+
return null;
142+
}
143+
139144
if (clrType != null
140145
&& _clrTypeMappings.TryGetValue(clrType, out var mapping))
141146
{

src/EFCore/Storage/TypeMappingInfo.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public TypeMappingInfo(
181181
var mappingHints = customConverter?.MappingHints;
182182
var property = principals[0];
183183

184+
ElementTypeMapping = property.GetElementType()?.FindTypeMapping();
184185
IsKeyOrIndex = property.IsKey() || property.IsForeignKey() || property.IsIndex();
185186
Size = fallbackSize ?? mappingHints?.Size;
186187
IsUnicode = fallbackUnicode ?? mappingHints?.IsUnicode;
@@ -195,17 +196,19 @@ public TypeMappingInfo(
195196
/// Creates a new instance of <see cref="TypeMappingInfo" />.
196197
/// </summary>
197198
/// <param name="member">The property or field for which mapping is needed.</param>
199+
/// <param name="elementTypeMapping">The type mapping for elements, if known.</param>
198200
/// <param name="unicode">Specifies Unicode or ANSI mapping, or <see langword="null" /> for default.</param>
199201
/// <param name="size">Specifies a size for the mapping, or <see langword="null" /> for default.</param>
200202
/// <param name="precision">Specifies a precision for the mapping, or <see langword="null" /> for default.</param>
201203
/// <param name="scale">Specifies a scale for the mapping, or <see langword="null" /> for default.</param>
202204
public TypeMappingInfo(
203205
MemberInfo member,
206+
CoreTypeMapping? elementTypeMapping = null,
204207
bool? unicode = null,
205208
int? size = null,
206209
int? precision = null,
207210
int? scale = null)
208-
: this(member.GetMemberType())
211+
: this(member.GetMemberType(), elementTypeMapping)
209212
{
210213
IsUnicode = unicode;
211214
Size = size;
@@ -217,6 +220,7 @@ public TypeMappingInfo(
217220
/// Creates a new instance of <see cref="TypeMappingInfo" />.
218221
/// </summary>
219222
/// <param name="type">The CLR type in the model for which mapping is needed.</param>
223+
/// <param name="elementTypeMapping">The type mapping for elements, if known.</param>
220224
/// <param name="keyOrIndex">If <see langword="true" />, then a special mapping for a key or index may be returned.</param>
221225
/// <param name="unicode">Specifies Unicode or ANSI mapping, or <see langword="null" /> for default.</param>
222226
/// <param name="size">Specifies a size for the mapping, or <see langword="null" /> for default.</param>
@@ -225,6 +229,7 @@ public TypeMappingInfo(
225229
/// <param name="scale">Specifies a scale for the mapping, or <see langword="null" /> for default.</param>
226230
public TypeMappingInfo(
227231
Type? type = null,
232+
CoreTypeMapping? elementTypeMapping = null,
228233
bool keyOrIndex = false,
229234
bool? unicode = null,
230235
int? size = null,
@@ -233,6 +238,7 @@ public TypeMappingInfo(
233238
int? scale = null)
234239
{
235240
ClrType = type?.UnwrapNullableType();
241+
ElementTypeMapping = elementTypeMapping;
236242

237243
IsKeyOrIndex = keyOrIndex;
238244
Size = size;
@@ -272,8 +278,14 @@ public TypeMappingInfo(
272278
ClrType = converter.ProviderClrType.UnwrapNullableType();
273279

274280
JsonValueReaderWriter = source.JsonValueReaderWriter;
281+
ElementTypeMapping = source.ElementTypeMapping;
275282
}
276283

284+
/// <summary>
285+
/// The element type mapping of the mapping, if any.
286+
/// </summary>
287+
public CoreTypeMapping? ElementTypeMapping { get; init; }
288+
277289
/// <summary>
278290
/// Returns a new <see cref="TypeMappingInfo" /> with the given converter applied.
279291
/// </summary>

0 commit comments

Comments
 (0)