如何嵌套DTO?

时间:2015-12-14 16:49:12

标签: entity-framework serialization entity-framework-6 dto

我将此作为我的实体对象:

    public partial class RFID_Zones
    {
        public RFID_Zones()
        {
            this.RFID_ZonePoints = new HashSet<RFID_ZonePoints>();
        }

        public int PK_ZoneId { get; set; }
        public int PK_FK_ShipId { get; set; }
        public string ZoneName { get; set; }
        public string Color { get; set; }

        public virtual Ship Ship { get; set; }
        public virtual ICollection<RFID_ZonePoints> RFID_ZonePoints { get; set;}
}

我试图用这段代码来完成所有这些:

result = _db.RFID_Zones.Where(x => x.PK_FK_ShipId == shipId).Include(x => x.RFID_ZonePoints).ToList();

这有效,但我无法在没有获得循环引用错误的情况下序列化它。通过谷歌搜索,我发现我应该使用数据传输对象,所以我有这个:

public class ZoneDto
{
    public ZoneDto()
    {
        this.Zones = new List<RFID_ZonePoints>();
    }
    public int PK_ZoneId { get; set; }
    public int PK_FK_ShipId { get; set; }
    public string ZoneName { get; set; }
    public string Color { get; set; }
    public List<RFID_ZonePoints> Zones { get; set; }
}

并且:

var dto = zones.Select(x => new ZoneDto { PK_ZoneId = x.PK_ZoneId, PK_FK_ShipId = x.PK_FK_ShipId,
                        Color = x.Color, ZoneName = x.ZoneName, Zones = x.RFID_ZonePoints.ToList()});

我仍然有RFID_ZonePoints列表的问题。它是一个不同实体的列表。我怎样才能将它们转换为数据传输对象?

0 个答案:

没有答案