使用AutoMapper时如何忽略内部嵌套对象 [英] How to ignore an inner nested object when using AutoMapper

查看:191
本文介绍了使用AutoMapper时如何忽略内部嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有以下课程:

班级用户

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
}

Class User DTO

Class User DTO

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

我进行了将用户映射到UserDTO的配置

I did the configurations to Map User to UserDTO

Mapper.CreateMap<User, UserDTO>();

由于实体框架一对一关系,我需要具有 Profile.User ,但我不希望Profile.User显示在映射中.

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.

如何忽略Profile.User?

How can I ignore the Profile.User?

推荐答案

您可以使用忽略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>();

这篇关于使用AutoMapper时如何忽略内部嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆