How to ignore an inner nested object when using AutoMapper

橙三吉。 提交于 2019-12-08 02:47:35

问题


Hello I have the classes:

Class User

public class User
{
    public Int64 Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }        
    public Profile Profile { get; set; } //EF one to one
}

Class Profile

    public class Profile 
{
    public Int64 Id { get; set; }
    public string Skype { get; set; }
    public string Phone { get; set; }
    public string Mobile { get; set; }
    public virtual ICollection<Address> Addresses { get; set; }
    public virtual User User { get; set; } //This is because EF Mappings
}

Class User DTO

public class UserDTO
{
    public string Name { get; set; }
    public string Email { get; set; }        
    public Profile Profile { get; set; }
}

I did the configurations to Map User to UserDTO

Mapper.CreateMap<User, UserDTO>();

I need to have the Profile.User because of the Entity Framework One to One Relationship but I don't want the Profile.User to be shown in the Mapping.

How can I ignore the Profile.User?


回答1:


You could use a UserProfileDTO class that omits User

public class UserProfileDTO
{
    public string Skype { get; set; }
    public string Phone { get; set; }
    public string Mobile { get; set; }
    public ICollection<AddressDTO> Addresses { get; set; }
}

public class UserDTO
{
    public string Name { get; set; }
    public string Email { get; set; }
    public UserProfileDTO Profile { get; set; }
}

Mapper.CreateMap<User, UserDTO>();
Mapper.CreateMap<Profile, UserProfileDTO>();


来源:https://stackoverflow.com/questions/31882969/how-to-ignore-an-inner-nested-object-when-using-automapper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!