0

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

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
SolarX
  • 1,873
  • 2
  • 18
  • 26

2 Answers2

1

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.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Hi, if I have a navigation property "Reports" on an "Investment" and Report is also derived from "BaseEntity", this navigation property returns an EntityCollection. This is fine. I might have confused ObhectSet and EntityCollection in regards of "Count()" on the cached entities. Thank you very much for your help! – SolarX Apr 20 '11 at 08:02
0

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.

Slauma
  • 175,098
  • 59
  • 401
  • 420
  • I understand that! But a simple ObjectSet like you have for non inherited entities let's you add objects and keeps them in memory. If you add an "Investment" with _container.BaseEntities.AddObject(investment) and you do _container.BaseEntities.OfType().ToList, the added "Investment" insn't in that list, because it's not saved to the database yet. Thus it would be much easier if I could have an ObjectSet ! – SolarX Apr 19 '11 at 13:26
  • 1
    @SolarX: Sorry, then I misinterpreted the background of your question. Would `_container.BaseEntities.ToList()` return the added but not yet saved `Investment` entity? Anyway, you should edit this point into your question, it makes the reason behind the question clearer, and perhaps someone knows a workaround. – Slauma Apr 19 '11 at 15:08
  • Not to worry :) EF jut made me a bit edgy today ;) Thank you very much for your assistance! – SolarX Apr 19 '11 at 15:28