Is it possible to configure Entity Framework Core >= 7.0 to use List<T> instead of HashSet<T> by configuration?

238 Views Asked by At

Entity Framework >= 7.0 provides HashSet<T> as implementation to 1-n and m-n associations (i.e. collections). In my case, these associations are typed as ICollection<T>, and HashSet<T> doesn't work for me, because JSON PATCH (RFC 6902) standard, it requires list data structure semantics (e.g. indexed collection, FIFO, etc.), when it comes to patching nested collections (i.e. arrays).

Therefore, I'd like to avoid a massive refactor which would involve typing associations as IList<T>, to leave DTOs agnostic to JSON PATCH implementation details.

Is it possible to configure Entity Framework Core to use List<T> instead of HashSet<T> by configuration (Code First)?

1

There are 1 best solutions below

2
Jakub On

Have you already checked new interceptor - IMaterializationInterceptor ? https://devblogs.microsoft.com/dotnet/announcing-ef7/#materialization-interception.

Maybe it'll allow you to modify property after initialization? Anyway, I'm not sure about navigation properties - but, you should give it a try and check it's possibilities.