var theList = DataContext.Fleets .Include(x => x.Vehicles.Select(y => y.EngineData)) // Not specific to Car or Truck .Include(x => x.Vehicles.OfType<Car>().Select(y => y.BultinEntertainmentSystemData)) // Only Cars have BultinEntertainmentSystemData .ToList();
因此,我希望获得所有车辆,包括车辆是汽车时内置娱乐系统的信息.我已经看到,如果我直接从DbSet开始,这是可行的,但在这里我正在查看Fleet对象的集合属性.当我在集合属性上使用带有OfType()调用的Include()调用时,我会收到此异常消息:
The Include path Expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation propertIEs and the Select operator for collection navigation propertIEs.
是否可以在集合属性中包含()子类型的属性?
解决方法 你不能做你正在尝试的,因为你正在编写的查询被“翻译”为sql. sql中没有“OfType”方法,并且OfType未设置为对象的属性,因此会出现此错误.理想情况下,EF会将此转换为仅表示从包含Type Car的表中获取数据,但事实并非如此.如果在父项上添加指示类型的属性,则可以使用该属性进行导航.
public class Vehicle{ public string Type {get; set;} //you need the setter here for EF}public class Car : Vehicle{ public Car() { Type = "Car"; }}DataContext.Fleets .Include(x => x.Vehicles.Select(y => y.EngineData)) // Not specific to Car or Truck .Include(x => x.Vehicles.Where(x => x.Type == "Car").Select(y => y.BultinEntertainmentSystemData)) // Only Cars have BultinEntertainmentSystemData .ToList();
这只是我现在想出来的,你可能需要改进它.使用字符串来定义这样的东西可能并不理想,尝试将其作为枚举等等.
总结以上是内存溢出为你收集整理的c# – 实体框架:我可以包含()子类型的属性吗?全部内容,希望文章能够帮你解决c# – 实体框架:我可以包含()子类型的属性吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)