开发者

Entity Framework 4: Why can't I get an ObjectSet of derived entities rather than an ObjectQuery?

I have an "Investment" entity that is derived from "BaseEntity".

With _开发者_运维知识库container.BaseEntities.OfType<Investment>() I get an ObjectQuery that always queries the database.

Therefore I would rather have an ObjectSet<Investment>.

I can't understand why EF doesn't support this for derived entities... Or does it? ;)

If I would go ahead and create a "root entity" in EF (which would be silly) that has associations to all my derived entities, I would get EntityCollections for those entities through the navigation properties of that one root-entity. But there must be another way...

Cheers


That is how it works in EF ObjectContext API. If you try to create ObjectSet for derived entity you will get:

ArgumentException: There are no EntitySets defined for the specified entity type 'Investment'. If 'Investment' is a derived type, use the base type instead. Parameter name: TEntity

Also once you define inheritance there are no navigation properties to derived entities. The association which offers navigation property is changed to inheritance.

I also followed your former questions which is probably source of this one and I have to say I tried a lot but I can never get your behavior. Even if I call Count directly to ObjectSet I always get SQL query (checked in the profiler) and count of entities in the database - not in the set.


ObjectQuery<T> does not always query the database. It is only a query specification - in this case a specification to return all BaseEntities of type Investment. You can compose it with additional filters or orderby clauses or projections and so on. Nothing is executed in the database until you apply some greedy operator like ToList() or First() etc. or until you apply a foreach loop to fetch the results.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜