如何在使用AutoMapper时忽略内部嵌套对象
本文关键字:内部 嵌套 对象 AutoMapper | 更新日期: 2023-09-27 18:06:49
你好,我有类:
类用户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
}
类概要
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
}
类用户DTO
public class UserDTO
{
public string Name { get; set; }
public string Email { get; set; }
public Profile Profile { get; set; }
}
我做了配置映射用户到UserDTO
Mapper.CreateMap<User, UserDTO>();
我需要有配置文件。用户,因为实体框架一对一的关系,但我不想要配置文件。要在映射中显示的用户。
如何忽略Profile.User?
您可以使用省略User
的UserProfileDTO
类
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>();