AutoMapper不在对象内映射对象

时间:2016-10-18 18:56:38

标签: c# .net automapper asp.net-core-mvc

AutoMapper目前没有拉下IpAddress对象中的Server对象。最终目标是在我的编辑视图中使用当前IP地址填充我的输入。但是,当我在Controller中放置一个断点时,IpAddress属性为null,因此我无法从中获取实际地址。我知道我在使用AutoMapper做错了什么,但还没找到具体的教程。提前谢谢!

Break point screenie

ServersController.cs

public async Task<IActionResult> Edit(int? id)
{
    if (id == null)
    {
        return NotFound();
    }

    var server = Mapper.Map<ServerViewModel>(await _context.Servers.SingleOrDefaultAsync(m => m.Id == id));
    if (server == null)
    {
        return NotFound();
    }
    return View(server);
}

ServerViewModel.cs

public class ServerViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public OperatingSystem OS { get; set; }
    public MachineType MachineType { get; set; }
    public string AdminUserName { get; set; }
    public string AdminPassword { get; set; }
    public string EsxHost { get; set; }
    public int IpAddressId { get; set; }
    public IpAddress IpAddress { get; set; }

    public List<SelectListItem> GetOperatingSystems()
    {
        List<SelectListItem> operatingSystems = new List<SelectListItem>();
        Array values = Enum.GetValues(typeof(OperatingSystem));
        operatingSystems.Add(new SelectListItem
        {
            Text = "Select",
            Value = ""
        });

        foreach(OperatingSystem val in values)
        {
            operatingSystems.Add(new SelectListItem
            {
                Text = val.ToString(),
                Value = val.ToString()
            });
        }
        return operatingSystems;
    }

    public List<SelectListItem> GetMachineTypes()
    {
        List<SelectListItem> machineTypes = new List<SelectListItem>();
        Array values = Enum.GetValues(typeof(MachineType));
        machineTypes.Add(new SelectListItem
        {
            Text = "Select",
            Value = ""
        });

        foreach (MachineType val in values)
        {
            machineTypes.Add(new SelectListItem
            {
                Text = val.ToString(),
                Value = val.ToString()
            });
        }
        return machineTypes;
    }
}

Server.cs

public class Server
{
    [Key]
    [Required]
    public int Id { get; set; }
    public string Name { get; set; }
    public OperatingSystem OS { get; set; }
    [Display(Name = "Machine Type")]
    public MachineType MachineType { get; set; }
    [Display(Name = "Admin User Name")]
    public string AdminUserName { get; set; }
    [Display(Name = "Admin Password")]
    public string AdminPassword { get; set; }
    [Display(Name = "ESX Host")]
    public string EsxHost { get; set; }
    public int IpAddressId { get; set; }
    [ForeignKey("IpAddressId")]
    public IpAddress IpAddress { get; set; }
}

Startup.cs

    Mapper.Initialize(config =>
    {
        config.CreateMap<IpAddressViewModel, IpAddress>().ReverseMap();
        config.CreateMap<ServerViewModel, Server>().ReverseMap();
    });

编辑:经过一些阅读,我发现我可以在查询中添加.Include()子句以引入IpAddress对象。我的新ServersController.cs代码如下。但是,我仍然有点困惑。这不是Automapper自动处理的事情吗?我错过了Automapper的全部内容吗?

NEW ServersController.cs

public async Task<IActionResult> Edit(int? id)
{
    if (id == null)
    {
        return NotFound();
    }

    var server = Mapper.Map<ServerViewModel>(await _context.Servers.Include(s => s.IpAddress).SingleOrDefaultAsync(m => m.Id == id));

    if (server == null)
    {
        return NotFound();
    }
    return View(server);
}

1 个答案:

答案 0 :(得分:0)

我将继续回答这个问题,以便对它进行一些封闭。

关键是在我的LINQ查询中添加.Include()子句以引入附加到父对象的对象。

正确的方法是在上面的编辑部分。可以找到更多示例here