使用ef版本 6.1.3 和 Unity 存储库在 asp.net mvc5上工作。假设有一个模型,其基本类型为SmartDevice和三种派生类型SmartRainbow、SmartRouter、SmartSwitch。像下面这样:
private readonly IRepositoryAsync<SmartDevice> _smartDeviceRepository;
构造函数具有以下语法
_smartDeviceRepository = unitOfWork.RepositoryAsync<SmartDevice>();
智能设备
public class SmartDevice : Entity
{
#region Primitive Properties
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DeviceId { get; set; }
public DeviceType DeviceType { get; set; }
#endregion
#region Navigation Properties
public virtual ICollection<DeviceStatus> DeviceStatus { get; set; }
#endregion
}
智能开关
public class SmartSwitch : SmartDevice
{
#region Navigation Properties
public virtual ICollection<Channel> Channels { get; set; }
#endregion
}
智能彩虹
public class SmartRainbow : SmartDevice
{
#region Navigation Properties
public virtual ICollection<RgbwStatus> RgbwStatuses { get; set; }
#endregion
}
通过查询SmartDevice我得到所有实体,我的语法如下:
var temp = _smartDeviceRepository.Query().Select().ToList();
问题是通过上述结果的每个实体都将是SmartRainbow或SmartRouter或SmartSwitch类型,我无法加载任何单个继承的类型属性和导航属性。
使用下面的语法我无法加载导航属性
var temp = _smartDeviceRepository.Query().Select().OfType<SmartSwitch>().ToList();
- 无法加载SmartSwitch属性Channels?
- 不能在OfType方法后使用include吗?
- 为什么不能在OfType之后包含?