Question

Using an easy example:

I have a class Car, with subclass Mazda.

Mazda has a property: MazdaWarrentyDate

Using my Db Context, I go:

var collection = db.user.cars;

I then, loop through collectiong using: var c in collection

I use a conditional to check if the type of instance is a mazda and then cast it to a mazda. But the MazdaWarrentyDate is null!

Upon debugging, I looked at the POCO before casting it, and indeed this property is not present, even though it recognised it as a type Mazda.

Is this a limitation in Entity framework? If so, how can I get around this? Thank you!

Was it helpful?

Solution 2

The reason the associate object was null, was because I started by querying at user -> car etc. To fix it, I simply started by querying at cars, e.g:

db.cars.where(c => c.user.id == myUserId);

Doing this caused the related poco's to be lazy loaded and this can all be iterated in one loop.

OTHER TIPS

var collection = db.user.cars.Where(t=> t is Mazda).OfType<Mazda>();

foreach (var car in collection ){

    //TODO 

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top