18253371 avatar image
0 Votes"
18253371 asked 18253371 edited

Entity Framework Core Custom Enum Key Dictionary Converter implementation

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.

            .Property(e => e.CATE_S_NM)
                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.


image.png (74.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers