带着孩子回归实体

时间:2016-08-13 09:03:19

标签: entity-framework asp.net-web-api asp.net-core

您好我正在尝试使用ASP.Net Core使用以下代码通过api返回所有带有记录里程数的车辆:

// GET: api/values
    [HttpGet]
    public IEnumerable<Vehicle> Get()
    {
        return _context.Vehicles.Include(m=>m.Mileages).ToList();
    }

然而,这只会返回第一辆带有里程数的车辆而不是其他车辆(数据库中有五辆虚拟车辆都有初始里程数。)

如果我将代码更改为:

// GET: api/values
    [HttpGet]
    public IEnumerable<Vehicle> Get()
    {
        return _context.Vehicles.ToList();
    }

它返回完整的车辆清单,但没有里程数。

我的班级文件是:

public class Vehicle
{
    public Vehicle()
    {
        Mileages = new List<Mileage>();
    }

    public int Id { get; set; }
    public string Registration { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public Marked Marked { get; set; }
    public ICollection<Mileage> Mileages { get; set; }
}

public class Mileage
{
    public int Id { get; set; }
    public DateTime MileageDate { get; set; }
    public string RecordedMileage { get; set; }

    //Navigation Properties
    public int VehicleId { get; set; }
    public Vehicle Vehicle { get; set; }
}

谢谢你的期待!

Tuppers

2 个答案:

答案 0 :(得分:0)

您可以使用代理自动加载(延迟加载)...但为此,您的外国实体和集合必须在您的POCO中标记为虚拟:

public class Mileage
{
    public int Id { get; set; }
    public DateTime MileageDate { get; set; }
    public string RecordedMileage { get; set; }
    //Navigation Properties
    public int VehicleId { get; set; }
    public virtual Vehicle Vehicle { get; set; }
}

public class Vehicle
{
    public Vehicle()
    {
        Mileages = new List<Mileage>();
    }

    public int Id { get; set; }
    public string Registration { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public Marked Marked { get; set; }
    public virtual ICollection<Mileage> Mileages { get; set; }
}

代理创建和延迟加载已启用,但这是EF6中的默认设置。 https://msdn.microsoft.com/en-us/data/jj574232.aspx

让我知道这是否有效。

答案 1 :(得分:0)

经过大量搜索后,我设法找到了解决方案。我使用了以下内容:

[HttpGet]
    public IEnumerable<VehicleDto> Get()
    {
        var query = _context.Vehicles.Select(v => new VehicleDto
            {
                Registration = v.Registration,
                Make = v.Make,
                Model = v.Model,
                Marked = v.Marked,
                Mileages = v.Mileages.Select(m => new MileageDto
                    {
                        MileageDate = m.MileageDate,
                        RecordedMileage = m.RecordedMileage
                    })
                    .ToList(),
            })
            .ToList();

        return (IEnumerable<VehicleDto>) query.AsEnumerable();

这似乎不是最优雅的方式,如果有人可以提供任何建议,但它确实返回了所需的。

DTO的外观如下:

public class VehicleDto
{
    public string Registration { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public Marked Marked { get; set; }
    public ICollection<MileageDto> Mileages { get; set; }
}

public class MileageDto
{
    public DateTime MileageDate { get; set; }
    public string RecordedMileage { get; set; }
}

感谢您花时间看看这个

Tuppers