简体   繁体   中英

How to use Automapper with Object in Object

I want to Map two types of Object but I didn't find the way to do it.

User class

public partial class TUser
    {
        [Key]
        [Column("id")]
        public int Id { get; set; }
        [Column("login")]
        [StringLength(50)]
        public string Login { get; set; }
        [Column("password")]
        [StringLength(50)]
        public string Password { get; set; }
        [Column("role")]
        [StringLength(50)]
        public string Role { get; set; }
        [Column("isDeleted")]
        public bool? IsDeleted { get; set; }
        [Column("avatarUrl")]
        [StringLength(50)]
        public string AvatarUrl { get; set; }
        [Column("iso")]
        [StringLength(2)]
        public string Iso { get; set; }
        [Column("lastLogonDate", TypeName = "datetime")]
        public DateTime? LastLogonDate { get; set; }
        [Column("createdDate", TypeName = "datetime")]
        public DateTime? CreatedDate { get; set; }
        [Column("lastUpdatedDate", TypeName = "datetime")]
        public DateTime? LastUpdatedDate { get; set; }
        public byte[] PasswordHash { get; set; }
        public byte[] PasswordSalt { get; set; }

        [InverseProperty("IdNavigation")]
        public virtual TWorker TWorker { get; set; }
    }

UserForLogin Class

public class UserForLogin
    {
        
        public int Id { get; set; }       
        public string Login { get; set; }      
        public string Role { get; set; }   
        public string AvatarUrl { get; set; }       
        public string Iso { get; set; }      
        public TWorker TWorker { get; set; }
    }

TWorker class

public partial class TWorker
    {
        public TWorker()
        {
            TWorkerToWorkType = new HashSet<TWorkerToWorkType>();
            TrEventToWorker = new HashSet<TrEventToWorker>();
            TrWorkerToWorkerCategory = new HashSet<TrWorkerToWorkerCategory>();
        }

        [Key]
        [Column("id")]
        public int Id { get; set; }
        [Column("lastname")]
        [StringLength(50)]
        public string Lastname { get; set; }
        [Column("firstname")]
        [StringLength(50)]
        public string Firstname { get; set; }
        [Column("email")]
        [StringLength(50)]
        public string Email { get; set; }
        [Column("phone")]
        [StringLength(50)]
        public string Phone { get; set; }
        [Column("address")]
        [StringLength(50)]
        public string Address { get; set; }
        [Column("postcode")]
        [StringLength(50)]
        public string Postcode { get; set; }
        [Column("locality")]
        [StringLength(50)]
        public string Locality { get; set; }
        [Column("workerCategoryKey")]
        public int? WorkerCategoryKey { get; set; }
        [Column("sexe")]
        [StringLength(50)]
        public string Sexe { get; set; }

        [ForeignKey(nameof(Id))]
        [InverseProperty(nameof(TUser.TWorker))]
        public virtual TUser IdNavigation { get; set; }
        [InverseProperty("WorkerKeyNavigation")]
        public virtual ICollection<TWorkerToWorkType> TWorkerToWorkType { get; set; }
        [InverseProperty("WorkerKeyNavigation")]
        public virtual ICollection<TrEventToWorker> TrEventToWorker { get; set; }
        [InverseProperty("WorkerKeyNavigation")]
        public virtual ICollection<TrWorkerToWorkerCategory> TrWorkerToWorkerCategory { get; set; }
    }

AutoMapperProfiles class


 public AutoMapperProfiles()
        {
            CreateMap<TUser, UserForLogin>()
                .ForMember(
                    dest => dest.TWorker,
                    opt => opt.MapFrom(src => src.TWorker)
                );                         
        }

But TWorker is always null and I can't find what am I doing wrong? If I use TUser only to return my object without Automapper code, TWorker contains the values I want.

Thanks for your help:-)

You just need to implement the map for the subObject and autoMapper will handle it for you. To be precise, if you map a property to another property which has a different type, autoMapper will try to find a corresponding map.

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