简体   繁体   中英

Entityframework subclass does not contain it's properties upon enumerating

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!

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

foreach (var car in collection ){

    //TODO 

}

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, eg:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM