You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I also noticed that when a variable that is a HashSet<int> (for example) is included in the select expression, EF Core 8 & 9 throw errors that seem to be JSON-serialization related. Like in #35567, this issue doesn't seem to occur when a non-primitive variable is included in the select expression.
Please refer to the reproduction and the stack trace included below 🙂.
With setOfDouble as a HashSet<double>, the stack trace is:
An exception occurred while iterating over the results of a query for context type 'TestEfCore.Program+Db'.
System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.HashSet`1[System.Double]' to type 'System.Collections.Generic.IList`1[System.Double]'.
at Microsoft.EntityFrameworkCore.Storage.Json.JsonCollectionOfStructsReaderWriter`2.FromJsonTyped(Utf8JsonReaderManager& manager, Object existingObject)
at Microsoft.EntityFrameworkCore.Storage.Json.JsonValueReaderWriter`1.FromJson(Utf8JsonReaderManager& manager, Object existingObject)
at Microsoft.EntityFrameworkCore.Storage.Json.JsonValueReaderWriter.FromJsonString(String json, Object existingObject)
at lambda_method11(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at TestEfCore.Program.Main() in ~\Projects\TestWeb\TestEfCore\Program.cs:line 27
As an IReadOnlySet<double> instead:
An exception occurred while iterating over the results of a query for context type 'TestEfCore.Program+Db'.
System.MissingMethodException: Cannot dynamically create an instance of type 'System.Collections.Generic.IReadOnlySet`1[System.Double]'. Reason: Cannot create an instance of an interface.
at System.RuntimeType.ActivatorCache..ctor(RuntimeType rt)
at System.RuntimeType.ActivatorCache.Create(RuntimeType type)
at System.RuntimeType.IGenericCacheEntry`1.CreateAndCache(RuntimeType type)
at System.RuntimeType.CreateInstanceOfT()
at System.Activator.CreateInstance[T]()
at Microsoft.EntityFrameworkCore.Storage.Json.JsonCollectionOfStructsReaderWriter`2.FromJsonTyped(Utf8JsonReaderManager& manager, Object existingObject)
at Microsoft.EntityFrameworkCore.Storage.Json.JsonValueReaderWriter`1.FromJson(Utf8JsonReaderManager& manager, Object existingObject)
at Microsoft.EntityFrameworkCore.Storage.Json.JsonValueReaderWriter.FromJsonString(String json, Object existingObject)
at lambda_method11(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at TestEfCore.Program.Main() in ~\Projects\TestWeb\TestEfCore\Program.cs:line 27
Verbose output
EF Core version
9.0.1
Database provider
Microsoft.EntityFrameworkCore.SqlServer
Target framework
.NET 9.0.102
Operating system
Windows 11
IDE
Microsoft Visual Studio Professional 2022 x64 17.12.4
The text was updated successfully, but these errors were encountered:
Problem here is that primitive collection variables are now processed as JSON parameters (https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-9.0/whatsnew#parameterized-primitive-collections) However, our infrastructure requires JSON collections to be either arrays or implementing IList<T> (as we require them to be ordered) - in JsonCollectionOfStructsReaderWriter.FromJsonTyped we do hard cast to IList<TElement> which fails for Hashset<TElement>.
If Hashset<T> is used as a primitive collection on an entity, we throw a good exception:
The type 'HashSet<double>' cannot be used as a primitive collection because it is not an array and does not implement 'IList<double?>'. Collections of primitive types must be arrays or ordered lists.
We should client eval primitive collections that don't implement IList
Bug description
I also noticed that when a variable that is a
HashSet<int>
(for example) is included in the select expression, EF Core 8 & 9 throw errors that seem to be JSON-serialization related. Like in #35567, this issue doesn't seem to occur when a non-primitive variable is included in the select expression.Please refer to the reproduction and the stack trace included below 🙂.
EF core
7.0.20
runs the queries with no errors.EF core
8.0.12
&9.0.1
throw as described.Your code
Stack traces
With
setOfDouble
as aHashSet<double>
, the stack trace is:As an
IReadOnlySet<double>
instead:Verbose output
EF Core version
9.0.1
Database provider
Microsoft.EntityFrameworkCore.SqlServer
Target framework
.NET 9.0.102
Operating system
Windows 11
IDE
Microsoft Visual Studio Professional 2022 x64 17.12.4
The text was updated successfully, but these errors were encountered: