简体   繁体   English

枚举时,EntityFramework子类不包含其属性

[英]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 马自达拥有物业: MazdaWarrentyDate

Using my Db Context, I go: 使用我的Db上下文,我可以:

var collection = db.user.cars;

I then, loop through collectiong using: var c in collection 然后,我使用以下命令遍历collectiong: 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. 我使用条件检查实例类型是否为mazda,然后将其转换为mazda。 But the MazdaWarrentyDate is null! 但是MazdaWarrentyDate为空!

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. 调试后,我在投射POCO之前先对其进行了研究,尽管该属性将其识别为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: 关联对象为null的原因是因为我从用户-> car等处开始查询。要解决此问题,我只是从在cars处开始查询,例如:

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. 这样做导致相关的poco被延迟加载,并且所有这些都可以在一个循环中进行迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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