实体框架4:为什么我不能得到派生实体的ObjectSet而不是ObjectQuery? [英] Entity Framework 4: Why can't I get an ObjectSet of derived entities rather than an ObjectQuery?
问题描述
使用 _container.BaseEntities.OfType<投资>()
我得到一个总是查询数据库的ObjectQuery。
因此,我宁愿拥有一个 ObjectSet< Investment> code>。
我不明白为什么EF不支持派生实体...还是这样做? ;)
如果我将继续在EF(这将是愚蠢的)创建一个根实体,它与我所有的派生实体有关联,我将获得EntityCollections通过该一个根实体的导航属性为这些实体。但是必须有另一种方式...
干杯
这就是它在EF ObjectContext API中的工作原理。如果您尝试为派生实体创建 ObjectSet
,您将获得:
ArgumentException:没有为指定的
实体类型投资定义的
EntitySets。如果
'Investment'是派生类型,则使用
代替基类型。参数名称:
TEntity
此外,一旦定义了继承,派生实体就没有导航属性。提供导航属性的关联更改为继承。
我还遵循您的,我不得不说我尝试了很多,但我永远不会得到你的行为。即使我直接调用Count到 ObjectSet
,我总是得到SQL查询(检查分析器)和数据库中的实体的数量 - 不在集合中。
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.
这篇关于实体框架4:为什么我不能得到派生实体的ObjectSet而不是ObjectQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!