Entity Framework Core Custom Enum Key Dictionary Converter implementation

vincent219 256 Reputation points
2021-10-12T08:47:13.133+00:00

I made an SDK that uses Cosmos DB via EF Core.
I created a Service by accessing Cosmos DB from the SDK,
I used this Service in Isolate Azure Timer Trigger. Then the following error occurred.

The Property {property} is a collection or enumeration type with a value converter but with no value comparer. Set a value comparer to ensure the collection/enumeration elements are compared correctly.

The above warning was not left by me, it was launched by Azure Function. No exceptions were thrown, the code worked all the way to the end, and the data entered Cosmos DB properly.

After searching and searching, it seems that a converter must be specified for the Enum value through HasConversion in the EF Core Context.
My code is below.

   modelBuilder.Entity<Qoo10JpCategory>()  
               .Property(e => e.CATE_S_NM)  
               .HasConversion(  
                   v => JsonConvert.SerializeObject(v),  
                   v => JsonConvert.DeserializeObject<Dictionary<ELanguage, string>>(v));  

ELanguage is an Enum type.
There is a thing called 'EnumToStringConverter', but this is for converting Enum to String, and this doesn't seem to be used when Enum is used as a key in Dictionary.
The data is corrected correctly, but I can't ignore these warnings.
How can I define an EF Core ValueConverter when using Enum as Key in Dictionary? Or is there something already defined?

Please Reply. Thanks.

139738-image.png

Developer technologies .NET Entity Framework Core
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.