JED
JED

Reputation: 1694

AutoMapper Asp.Net Core 2: Unmapped Properties

When trying to map my Dto to my model, I am getting an error that certain properties are not being mapped.

Unmapped properties:
Id
AddressId
ContactId
Barns
Gateways
UserFarms


But the Id is supposed to be auto-generated, the AddressId, ContactId, and Barns are nullable properties.

The issue occurs when I get to var farmToCreate = _mapper.Map<Farm>(farmForCreateDto); within the controller. It tries to log the user activity which runs a SaveChangesAsync on the database, and, at that point, throws an error that it can't map the Farm Dto to its Model due to unmapped properties.


FarmsController.cs

[ServiceFilter(typeof(LogUserActivity))]
[Authorize]
[Route("api/[controller]")]
public class FarmsController : Controller
{
    private readonly IBaseRepository _repo;
    private readonly IMapper _mapper;

    public FarmsController(IBaseRepository repo, IMapper mapper)
    {
        _mapper = mapper;
        _repo = repo;
    }

    [HttpPost("createFarm")]
    public async Task<IActionResult> CreateFarm([FromBody] FarmForCreateDto farmForCreateDto)
    {
        if (!ModelState.IsValid)
            return BadRequest(ModelState);

        Address createdFarmAddress = null;

        if (farmForCreateDto.Address != null)
        {
            var farmAddressToCreate = _mapper.Map<Address>(farmForCreateDto.Address);

            createdFarmAddress = await _repo.CreateAddress(farmAddressToCreate);
        }

        Address createdFarmContactAddress = null;

        if (farmForCreateDto.Contact.Address != null)
        {
            var farmContactAddressToCreate = _mapper.Map<Address>(farmForCreateDto.Contact.Address);

            createdFarmContactAddress = await _repo.CreateAddress(farmContactAddressToCreate);
        }

        Contact createdFarmContact = null;

        if (farmForCreateDto.Contact != null)
        {
            var farmContactToCreate = _mapper.Map<Contact>(farmForCreateDto.Contact);

            farmContactToCreate.Address = createdFarmContactAddress;

            createdFarmContact = await _repo.CreateContact(farmContactToCreate);
        }

        var farmToCreate = _mapper.Map<Farm>(farmForCreateDto);

        farmToCreate.Address = createdFarmAddress;
        farmToCreate.Contact = createdFarmContact;

        var createdFarm = await _repo.CreateFarm(farmToCreate);

        var farmToReturn = _mapper.Map<FarmForDetailedDto>(createdFarm);

        return CreatedAtRoute("GetFarm", new { controller = "Farms", id = createdFarm.Id }, farmToReturn);
    }


Models\Farm.cs

public class Farm
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int? AccountId { get; set; }
    public Account Account { get; set; }

    public int? AddressId { get; set; }
    public Address Address { get; set; }

    public int? ContactId { get; set; }
    public Contact Contact { get; set; }

    public ICollection<Barn> Barns { get; set; }
}


Dtos\FarmForCreateDto.cs

public class FarmForCreateDto
{
    [Required]
    public string Name { get; set; }

    public Models.Account Account { get; set; }

    public ContactForCreateDto Contact { get; set; }

    public AddressForCreateDto Address { get; set; }
}

Upvotes: 0

Views: 803

Answers (1)

Antonio Campagnaro
Antonio Campagnaro

Reputation: 558

You need to configure mappings.

How to configure mappings

Upvotes: 1

Related Questions