Skip to content

Commit 47800f6

Browse files
committed
Fix for object collections
1 parent c7f960d commit 47800f6

File tree

4 files changed

+12
-16
lines changed

4 files changed

+12
-16
lines changed

src/EFCore.Relational/Storage/RelationalTypeMappingSource.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,12 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
235235
new ValueConverterInfo(modelType, typeof(string), _ => null!)))!
236236
.Clone(
237237
(ValueConverter)Activator.CreateInstance(
238-
typeof(CollectionToJsonStringConverter<>).MakeGenericType(
239-
elementType),
240-
collectionReaderWriter!)!,
238+
typeof(CollectionToJsonStringConverter<>).MakeGenericType(elementType), collectionReaderWriter!)!,
241239
(ValueComparer?)Activator.CreateInstance(
242240
elementType.IsNullableValueType()
243241
? typeof(NullableValueTypeListComparer<>).MakeGenericType(elementType.UnwrapNullableType())
244-
: typeof(ListComparer<>).MakeGenericType(elementType),
245-
elementMapping?.Comparer),
242+
: typeof(ListComparer<>).MakeGenericType(elementMapping!.Comparer.Type),
243+
elementMapping!.Comparer),
246244
elementMapping,
247245
collectionReaderWriter)
248246
: null;

src/EFCore/ChangeTracking/ListComparer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private static IList<TElement> Snapshot(IEnumerable<TElement> source, ValueCompa
112112
typeof(IList<>).MakeGenericType(elementComparer.Type).ShortDisplayName()));
113113
}
114114

115-
if (sourceList.GetType().IsArray)
115+
if (sourceList.IsReadOnly)
116116
{
117117
var snapshot = new TElement[sourceList.Count];
118118

@@ -129,7 +129,7 @@ private static IList<TElement> Snapshot(IEnumerable<TElement> source, ValueCompa
129129
}
130130
else
131131
{
132-
var snapshot = source is List<TElement>
132+
var snapshot = (source is List<TElement> || sourceList.IsReadOnly)
133133
? new List<TElement>(sourceList.Count)
134134
: (IList<TElement>)Activator.CreateInstance(source.GetType())!;
135135

src/EFCore/ChangeTracking/NullableValueTypeListComparer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private static bool Compare(IEnumerable<TElement?>? a, IEnumerable<TElement?>? b
8787
CoreStrings.BadListType(
8888
(a is IList<TElement?> ? b : a).GetType().ShortDisplayName(),
8989
typeof(NullableValueTypeListComparer<TElement>).ShortDisplayName(),
90-
typeof(IList<>).MakeGenericType(elementComparer.Type).ShortDisplayName()));
90+
typeof(IList<>).MakeGenericType(elementComparer.Type.MakeNullable()).ShortDisplayName()));
9191
}
9292

9393
private static int GetHashCode(IEnumerable<TElement?> source, ValueComparer<TElement> elementComparer)
@@ -110,10 +110,10 @@ private static int GetHashCode(IEnumerable<TElement?> source, ValueComparer<TEle
110110
CoreStrings.BadListType(
111111
source.GetType().ShortDisplayName(),
112112
typeof(NullableValueTypeListComparer<TElement>).ShortDisplayName(),
113-
typeof(IList<>).MakeGenericType(elementComparer.Type).ShortDisplayName()));
113+
typeof(IList<>).MakeGenericType(elementComparer.Type.MakeNullable()).ShortDisplayName()));
114114
}
115115

116-
if (sourceList.GetType().IsArray)
116+
if (sourceList.IsReadOnly)
117117
{
118118
var snapshot = new TElement?[sourceList.Count];
119119

@@ -127,7 +127,7 @@ private static int GetHashCode(IEnumerable<TElement?> source, ValueComparer<TEle
127127
}
128128
else
129129
{
130-
var snapshot = source is List<TElement?>
130+
var snapshot = source is List<TElement?> || sourceList.IsReadOnly
131131
? new List<TElement?>(sourceList.Count)
132132
: (IList<TElement?>)Activator.CreateInstance(source.GetType())!;
133133

src/EFCore/Storage/TypeMappingSource.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,12 @@ protected TypeMappingSource(TypeMappingSourceDependencies dependencies)
191191
new ValueConverterInfo(modelType, typeof(string), _ => null!)))!
192192
.Clone(
193193
(ValueConverter)Activator.CreateInstance(
194-
typeof(CollectionToJsonStringConverter<>).MakeGenericType(
195-
elementType!),
196-
collectionReaderWriter!)!,
194+
typeof(CollectionToJsonStringConverter<>).MakeGenericType(elementType), collectionReaderWriter!)!,
197195
(ValueComparer?)Activator.CreateInstance(
198196
elementType.IsNullableValueType()
199197
? typeof(NullableValueTypeListComparer<>).MakeGenericType(elementType.UnwrapNullableType())
200-
: typeof(ListComparer<>).MakeGenericType(elementType),
201-
elementMapping?.Comparer),
198+
: typeof(ListComparer<>).MakeGenericType(elementMapping!.Comparer.Type),
199+
elementMapping!.Comparer),
202200
elementMapping,
203201
collectionReaderWriter)
204202
: null;

0 commit comments

Comments
 (0)