0

I want to Map two types of Objects 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.

3
  • Can you show the code where you are triggering the mapping? Commented Jun 22, 2020 at 14:30
  • Why do you have this ForMember() line in there? The TWorker entity exists in both classes and the type matches. Please edit your question to include a minimal reproducible example, which can be tested by others. Commented Jun 22, 2020 at 14:44
  • docs.automapper.org/en/latest/Nested-mappings.html Commented Jun 22, 2020 at 16:47

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.