Base solution for your next web application
Open Closed

AutoMapper is not mapping the extended properties of User #803


User avatar
1
paul lee created

Hi there,

I have the following model:

public class User : AbpUser<Tenant, User>
  {
    // code omitted for brevity
    [ForeignKey("Employeer")]
    public virtual long? EmployeerId { get; set; }
    public virtual Company Employeer { get; set; }
  }

  public class Company : FullAuditedEntity<long>, IMustHaveTenant
  {
    // code omitted for brevity
    [Required]
    [StringLength(MaxCodeLength)]
    public virtual string Code { get; private set; }  
    [Required]
    [StringLength(MaxNameLength)]
    public virtual string Name { get; private set; }
    public virtual int TenantId { get; set; }

    public virtual ICollection<User> Employees { get; private set; }
  }

And I want to map this into the following DTO:

[AutoMapFrom(typeof(User))]
  public class UserLoginInfoDto : EntityDto<long>
  {
    public string Name { get; set; }
    public string Surname { get; set; }
    public string UserName { get; set; }
    public string EmailAddress { get; set; }
    public string EmployeerName { get; set; }
  }

But AutoMapper is not mapping the Company.Name into EmployeerName in the DTO. How can I make this happen?

Thanks


2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    I did many mapping like that and had no such a problem. I think this is not related to AutoMapper. See your property in Company:

    public virtual string Name { get; private set; }

    Probably, EF can not load Name because it's private. Change settet to public or protected and try again.

  • User Avatar
    0
    paul lee created

    It turns out to be a data problem, forgot to link the AbpUser with the default Company row, therefore, no Company name to be displayed.

    I haven't changed the private Set's on the Entity and it seems to be working just fine.

    Thank you very much!