简体   繁体   中英

Mapping Virtual Property Automapper

I have some problem with mapping models . So I have a entity model

public class User
{
    public string UserId { get; set;}

    public ICollection<Group> Groups {get; set;}
}

and DTO model

public class UserInfo
{   
    public string UserId { get; set;}  

    public List<GroupInfo> Groups {get; set;}
}

So I have problem when mapping User to UserInfo Missing configuration type for GroupInfo . How intialize second mapping ?

User is mapped to UserInfo as the following:

var config = new MapperConfiguratiins(cfg=>cfg.CreateMap<User,UserInfo>()); 
var mapper = config.CreateMapper();
var userInfo = mapper.Map<UserInfo>(user);

Try this for your MapperConfiguration :

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<Group, GroupInfo>();
    cfg.CreateMap<User, UserInfo>();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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