Why does EF Core treat the conversion method of HashSet<int> differently? #114858
Answered
by
huoyaoyuan
zms9110750
asked this question in
Q&A
-
/*
modelBuilder.Entity<TestEntity>()
.Property(e => e.ArrayProperty)
.HasConversion(v => JsonSerializer.Serialize(v, (JsonSerializerOptions)null!),
s => JsonSerializer.Deserialize<HashSet<int>>(s, (JsonSerializerOptions)null!)!);
*/
modelBuilder.Entity<TestEntity>()
.Property(e => e.ArrayProperty)
.HasConversion(v => v.Select(s=>s),
s => s.ToHashSet());
//Unhandled exception. System.InvalidOperationException: The type 'HashSet<int>' cannot be used as a primitive collection
//because it is not an array and does not implement 'IList<int?>'. Collections of primitive types must be arrays or ordered lists. The above code is workable. modelBuilder.Entity<TestEntity>()
.Property(s => s.PPT)
.HasConversion(s => (s.Values.Select(s => s.ID))
, s => s.Select(s => TestEntity4s.First(l => l.ID == s)).ToDictionary(s => s.ID)); |
Beta Was this translation helpful? Give feedback.
Answered by
huoyaoyuan
Apr 21, 2025
Replies: 1 comment
-
Please post EF Core questions to https://github.com/dotnet/efcore. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
zms9110750
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please post EF Core questions to https://github.com/dotnet/efcore.