简体   繁体   English

AutoMapper不在对象内映射对象

[英]AutoMapper not mapping object within object

AutoMapper is currently not pulling down the IpAddress object within my Server object. AutoMapper当前未在我的Server对象中下拉IpAddress对象。 The end goal is go have my input already populated with the current IP Address in my edit view. 最终目标是在我的编辑视图中用当前IP地址填充我的输入。 However, when I place a break point in my Controller, the IpAddress property is null so I can't get the actual address out of it. 但是,当我在控制器中放置一个断点时, IpAddress属性为null,因此无法从中获取实际地址。 I know I am doing something incorrectly with AutoMapper, but haven't been able to find a specific tutorial yet. 我知道我在AutoMapper中做错了什么,但是还没有找到特定的教程。 Thanks in advance! 提前致谢!

Break point screenie 断点筛选

ServersController.cs 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 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 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();
    });

EDIT: After some reading, I have discovered that I can add a .Include() clause to my query to bring in the IpAddress object. 编辑:经过一番阅读,我发现我可以在我的查询中添加一个.Include()子句以引入IpAddress对象。 My new ServersController.cs code is below. 我的新ServersController.cs代码如下。 However, I am still a little confused. 但是,我还是有些困惑。 Isn't this the type of thing that Automapper was meant to take care of automatically? 这不是Automapper打算自动处理的事情吗? Am I missing the entire point of Automapper? 我是否想念Automapper的全部内容?

NEW ServersController.cs 新的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);
}

I am going to go ahead and answer this to put some closure on it. 我将继续回答这个问题,以解决此问题。

The key was adding a .Include() clause to my LINQ query to pull in objects attached to the parent object. 关键是在我的LINQ查询中添加了一个.Include()子句,以提取附加到父对象的对象。

The correct way to do this is in my EDIT section above. 正确的方法是在上面的“编辑”部分中。 Further examples can be found here . 可以在此处找到更多示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM