1

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)?

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • 2
    Please post the code. Navigation properties can already be declared as `List` on the many side. – Tanveer Badar Dec 30 '22 at 18:34
  • 1
    Are you trying to reverse engineer from an existing database? EF does allow the usage of `List` if you are configuring the entities by hand. – Eldar Dec 30 '22 at 18:39
  • Both @TanveerBadar and @Eldar I believe you don't understand the issue at all. If you double-check my question, I'm asking to avoid typing properties as `IList`. I know this approach, but I'm trying to check if there's some way to configure default collection which EF sets to `ICollection`-typed associations, to provide `List` instead of `HashSet`. – Matías Fidemraizer Jan 02 '23 at 13:20
  • Can you expand and clarify the question a little? Are you saying you want to use Entity Framework as your DTO layer? You want to keep associations typed as ICollection rather than IList because changing those property types would require a massive refactor? But you need the collection to be indexed numerically? Are you using a library to do JSON PATCH that could work with an `ICollection`-typed property if it were backed by a numerically-indexed collection at run-time? Are you aware HashSet in .NET has FIFO semantics? It's not clear how the solution you seek solves the problem you describe. – StriplingWarrior Apr 17 '23 at 16:18

1 Answers1

-1

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.

Posio
  • 962
  • 4
  • 15
  • Disclamer, I've not downvoted your answer. Anyway, I'm asking about if there's some way to configure this by code during initialization, something like configuring default collection implementation. – Matías Fidemraizer Jan 02 '23 at 18:32
  • I think HashSet is a default EF implementation of ICollection and I havent found a away to change it easily so far. That's why I suggested you to check for `IMaterializationInterceptor` - maybe there is a way to change the navigation property from HashSet to List after initialization of the object. – Posio Jan 03 '23 at 08:27