0

I am trying to map the FK relation in my DTO class. However, I always get 'null' values as response in Postman.

Since the project includes companies that can be either clients/ partners etc. I have created a base table of company and created clients with FK relationship to company.

Company Class

public class Company
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [MaxLength(50)]
    public string Name { get; set; }

    [MaxLength(50)]
    public string Street { get; set; }

    public int Zip { get; set; }

    [MaxLength(50)]
    public string City { get; set; }

Client Class

public class Client
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [ForeignKey("CompanyId")]
    public Company Company { get; set; }
    public int CompanyId { get; set; }

    public ICollection<User> Users { get; set; }
        = new List<User>();

    public ICollection<Department> Departments { get; set; }
        = new List<Department>();

ClientDTO

public class ClientDto
{
    public int Id { get; set; }
    public string CompanyName { get; set; }
    public string CompanyStreet { get; set; }

    public ICollection<UserDto> Users { get; set; }
        = new List<UserDto>();
}

AutoMapper in Startup Class

Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<Client, ClientDto>();
            cfg.CreateMap<Client, ClientWithoutUsersDto>();
            cfg.CreateMap<Company, CompanyDto>();
            cfg.CreateMap<User, UserDto>();
        });

Whatever mapping I try I get the same Json Response in Postmane:

"id": 1,
"companyName": null,
"streetName": null

I am not sure whether Automapper supports nested FK mapping. I know that the User Collection works well.

4
  • How do you perform the mapping - with Map or with ProjectTo? If Map, are you loading the Client.Company property? It won't be populated by default, you need to Include it - see Loading Related Data Commented Mar 5, 2019 at 1:28
  • Thanks Ivan, I am using Map for the mapping. Where do I need to load the client.company? in the Client class ? Commented Mar 5, 2019 at 1:41
  • What is your query code for client data? Commented Mar 5, 2019 at 6:37
  • Try ProjectTo instead. Commented Mar 5, 2019 at 8:24

1 Answer 1

2

Check your query command whether it return the expected object Company.

Here is a mini-working demo for query.

var client = _context.Client.Include(c => c.Company).FirstOrDefault(c => c.Id == 1);
Mapper.Initialize(cfg => cfg.CreateMap<Client, ClientDto>());
var result = Mapper.Map<ClientDto>(client);
Sign up to request clarification or add additional context in comments.

1 Comment

The MapFroms are not needed.

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.