Search code examples
c#automapperaspnetboilerplate

What can cause ObjectMapper.Map to delete entity rather than update in service?


I have an issue with the Update method shown below. Create is working just fine but when I attempt to Update an entity in this specific application service, it is deleting rather than updating.

Can someone clue me in on what might be wrong with the call and why it would be deleted rather than updated? I have multiple Create and Update methods in other services that work exactly the same as this one, but are not giving me any issues.

I am using ASP.NET Boilerplate v3.5.

Call to app service:

var map = new CreateOrEditStoreAddressMapDto
{
    Id = 10008
    StoreId = 1000,
    AddressId = 1,
    PrimaryBilling = false,
    PrimaryShipping = true
};

await _storeAddressMapAppService.CreateOrEdit(map);

Table:

[Table("StoreAddressMappings")]
public class StoreAddressMap : CreationAuditedEntity<long>
{
    public virtual long AddressId { get; set; }

    [ForeignKey("AddressId")]
    public virtual Address Address { get; set; }

    public virtual int StoreId { get; set; }

    [ForeignKey("StoreId")]
    public virtual Store Store { get; set; }

    public virtual bool PrimaryBilling { get; set; }

    public virtual bool PrimaryShipping { get; set; }
}

DTO:

public class CreateOrEditStoreAddressMapDto : CreationAuditedEntityDto<long>
{
    public long AddressId { get; set; }

    public AddressDto Address  {get;set;}

    public int StoreId { get; set; }

    public StoreDto Store { get; set; }

    public bool PrimaryBilling { get; set; }

    public bool PrimaryShipping { get; set; }
}

App service:

public async Task CreateOrEdit(CreateOrEditStoreAddressMapDto input)
{
    if (input.Id.HasValue) {
        await Create(input);
    } else {
        await Update(input);
    }
}

[AbpAuthorize(AppPermissions.Pages_Administration_StoreAddressMappings_Create)]
private async Task Create(CreateOrEditStoreAddressMapDto input)
{
    var storeAddressMap = ObjectMapper.Map<StoreAddressMap>(input);
    await _storeAddressMapRepository.InsertAsync(storeAddressMap);
}

// THIS IS THE FUNCTION THAT DELETES THE OBJECT RATHER THAN UPDATE IT
[AbpAuthorize(AppPermissions.Pages_Administration_StoreAddressMappings_Edit)]
private async Task Update(CreateOrEditStoreAddressMapDto input)
{
    var storeAddressMap = await _storeAddressMapRepository.FirstOrDefaultAsync((long) input.Id);
    ObjectMapper.Map(input, storeAddressMap);
}

Custom DTO mapper:

configuration.CreateMap<CreateOrEditStoreAddressMapDto, StoreAddressMap>();

Solution

  • I was able to figure it out. The DTO has the AddressDto and StoreDto and because those were NULL it was causing the delete.