0

It is easy to map an entity to unonymous type or to the type with parameterless constructor:

context.EntitiesSet.Select( entity => new MyClass { Prop = entity.Id } );

But if MyClass implements IEnumerable interface then EF throws NotSupportedException.

I'd like to know what's the point for this oddity.

Andre Lombaard
  • 6,985
  • 13
  • 55
  • 96
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137

1 Answers1

1

I think it is related to this question: How do I load related entities of type IEnumerable<T>, where it is asked why navigation properties can't implement IEnumerable.

In the current EF 6 source there is a check whether a type is supported for materialization. There it only says in a comment

// types implementing IEnumerable are not supported

Not much of an explanation there.

However, for collection properties I can see why it makes sense to require at least ICollections, because it is essential that object can be added and removed from collections (IEnumerable has no Add and Delete methods).

I think that the case of a projected type implementing IEnumerable was either deemed too uncommon to support or just overlooked.

Community
  • 1
  • 1
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291