什么可以导致ObjectMapper.Map删除实体而不是在服务中更新?

时间:2018-03-22 13:40:55

标签: c# automapper aspnetboilerplate

我遇到了下面显示的Update方法的问题。 Create工作得很好但是当我尝试Update此特定应用程序服务中的实体时,它正在删除而不是更新。

有人可以告诉我这个电话可能出错的原因以及为什么要删除而不是更新?我在其他服务中有多个CreateUpdate方法与此方法完全相同,但没有给我任何问题。

我正在使用ASP.NET Boilerplate v3.5。

致电应用服务:

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

await _storeAddressMapAppService.CreateOrEdit(map);

表格

[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; }
}

应用服务:

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);
}

自定义DTO映射器:

configuration.CreateMap<CreateOrEditStoreAddressMapDto, StoreAddressMap>();

1 个答案:

答案 0 :(得分:0)

我能够弄清楚。 DTO具有AddressDto和StoreDto,因为它们是NULL,导致删除。