简体   繁体   English

自动映射器-嵌套列表

[英]Automapper - Nested Lists

I am working with AutoMapper, which I am relatively new with, and I stumbled upon a small mapping problem I was hoping the community could assist with. 我正在与AutoMapper一起工作,而我相对较新,所以偶然发现了一个我希望社区可以提供帮助的小型映射问题。

So I have two data transfer objects: 因此,我有两个数据传输对象:

public class UserDto {
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<CharacterDto> Characters { get; set; }
}

public class CharaterDto {
    public string CharacterName { get; set; }
    public string ClassName { get; set; }
    public int CharacterLevel { get; set; }
}

and two Domain Entities 和两个域实体

public class Character {
    public int ID { get; set; }
    public int UserId { get; set; }
    public string CharacterName { get; set; }
    public string ClassName { get; set; }
    public int CharacterLevel { get; set; }
}

public class User {
    public int ID { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

The end goal is to be able to save the data taken in by the DTOs into the database via the Domain Entities; 最终目标是能够通过域实体将DTO接收的数据保存到数据库中。 however, when it comes to typing up the list of Characters for 'UserDto', I do not know how to map this properly with AutoMapper. 但是,当涉及到“ UserDto”的字符列表时,我不知道如何使用AutoMapper正确映射它。 I can map it manually with little to no problems... but I can't find anything that helps to explain this or any examples that would help me understand it better. 我可以手动映射它,几乎没有问题...但是找不到任何有助于解释此问题的示例或任何可以帮助我更好地理解它的示例。

I have tried doing things like: 我曾尝试做类似的事情:

 CreateMap<UserDto, Character>()
     .ForMember(dest => dest.CharacterName, m => m.MapFrom(source => source.Characters[0].CharacterName));

However, this seems to only map the 1st entry and not the others. 但是,这似乎仅映射第一个条目,而不映射其他条目。 I have also considered mapping the individual mappings like so: 我也考虑过像这样映射各个映射:

 CreateMap<CharacterDto, Character>();
 CreateMap<UserDto, Character>()
     .ForMember(?/*this section I cannot figure out*/)

But can't figure out how to associate the the collection of characters to the mapped CharacterDto. 但是无法弄清楚如何将字符集合与映射的CharacterDto相关联。 I doubt that if I run the code without that association, the code is going to automatically understand that for each character in characters , map each character using the appropriate mapper... If I must manually do this, I can... but if there is an AutoMapper way, any help constructing it would be greatly appreciated. 我怀疑如果我在没有这种关联的情况下运行代码,那么代码是否会自动理解for each character in characters每个字符,请使用适当的映射器映射每个字符...如果必须手动执行此操作,则可以...但是如果有一种AutoMapper方式,对构建它的任何帮助将不胜感激。

Type converters are you friend here for mapping 1 to many like this. 类型转换器是您的朋友,可以将1映射到许多这样的对象。 Let me know if you need me to go further and get you a working example from your models. 让我知道是否需要我进一步讲解,并从您的模型中获得可行的示例。

https://stackoverflow.com/a/18096914/7911333 https://stackoverflow.com/a/18096914/7911333

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

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