zeeshan
zeeshan

Reputation: 79

ASP.NET Core 8 Web API : mapster does not flat object

I am using mapster for object mapping in an ASP.NET Core 8 Web API. But I am having issues when converting entity to DTO using mapster.

Here's my code :

public class Tenant : BaseEntity
{
    public Tenant()
    {
        Address = new Address();
    }

    public string TenantName { get; set; }
    public string Email { get; set; }
    public Address Address { get; set; }
}

public class TenantQueryDTO
{
    public string Description { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Name { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public object Id { get; internal set; }
}

public List<TenantQueryDTO> Get()
{
    var data = await repository.GetAllAsync(cancellationToken);

    return _mapper.Map<List<TenantQueryDTO>>(data); //address information  is not populated
}

This is the is mapper configuration:

static void MapTenantAndAddressDTO()
{
    TypeAdapterConfig<Tenant, TenantQueryDTO>
           .NewConfig()
           .Map(dest => dest.Id, src => src.Id)
           .Map(dest => dest.Name, src => src.TenantName)

           .Map(dest => dest.AddressLine1, src => src.Address.AddressLine1)

           .TwoWays();
}

Upvotes: 0

Views: 179

Answers (0)

Related Questions