Andre Evangelista
Andre Evangelista

Reputation: 3598

How to ignore an inner nested object when using AutoMapper

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?

Upvotes: 2

Views: 837

Answers (1)

Jasen
Jasen

Reputation: 14250

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>();

Upvotes: 4

Related Questions