【发布时间】:2017-02-13 10:30:09
【问题描述】:
我有一些带有子对象的类。当我检索数据并将其转换为 JSON(例如转换为 DeviceType 类)时,它也会返回来自相关子实体的数据。如何在不设置Ignore 属性或ProxyCreationEnabled 的情况下避免获取子实体的数据?在某些情况下,我还需要子类数据。
请参见下面的代码:当我为DeviceTypes 选择数据时,它也会返回Devices。现在我使用Select 只获取我需要的字段,但随着应用程序的增长,将难以处理。由于我使用的是动态类型,因此我只能将其作为对象返回。
public partial class DeviceType
{
public DeviceType()
{
this.Devices = new HashSet<Devices >();
}
public int Id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }
public string Code { get; set; }
public virtual ICollection<Device> Devices { get; set; }
}
public override ICollection<DeviceType> Get()
{
return DbContext.Set<DeviceType>().ToList().Select(x => new DeviceType
{
Id = x.Id,
Name = x.Name
}).ToList();
}
【问题讨论】:
标签: c# entity-framework linq model-view-controller