开发者

EF TPH Inheritance Query

Trying to implement a very simple TPH setup for a system I'm making, 1 base, 2 inherited classes.

However the inherited classes all belong to the same entity set, so within my ObjectContext using loop, I can only access the base abstract class. 开发者_JAVA技巧I'm not quite sure how I get the elements which are concrete classes? (I've also converted it to using POCO).

EF TPH Inheritance Query

Then within my application using the Entities:

using (SolEntities sec = new SolEntities()) {
    Planets = sec.CelestialBodies;
}

There's a CelestialBodies entity set on sec, but no Planets/Satellites as I'd expect.

Not quite sure what needs to be done to access them.

Thanks


You can use the OfType method:

using (SolEntities sec = new SolEntities()) {
    Planets = sec.CelestialBodies.OfType<Planet>();
}


As Thomas Levesque described OfType extension method will allow you querying only single inherited type you really want to access. If you access CelestialBodies directly you will get all entities. Every entity will be of type Planet or Satellite but you will have to cast them to access their properties.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜