Код: Выделить всё
public class PurchaseReq
{
...
public string PrNumber { get; set; } // primary key
public List
PurchaseReqItems { get; init; } = [];
public List PurchaseReqCharges { get; init; } = [];
}
public class PurchaseReqItem
{
...
public int Id { get; init; } // primary key
public string PrNumber { get; set; } // foreign key
public PurchaseReq PurchaseReq { get; init; } //navigation property
}
public class PurchaseReqCharge
{
...
public int Id { get; init; } // primary key
public string PrNumber { get; set; } // foreign key
public PurchaseReq PurchaseReq { get; init; } //navigation property
}
Код: Выделить всё
public class PurchaseReqDto
{
...
public string PrNumber { get; set; }
public List
PurchaseReqItems { get; init; } = [];
public List PurchaseReqCharges { get; init; } = [];
}
public class PurchaseReqItemDto
{
...
public int Id { get; init; }
public string PrNumber { get; set; }
public PurchaseReqDto PurchaseReq { get; init; }
}
public class PurchaseReqChargeDto
{
...
public int Id { get; init; }
public string PrNumber { get; set; }
public PurchaseReqDto PurchaseReq { get; init; }
}
Код: Выделить всё
public class PurchaseReqProfile : Profile
{
public PurchaseReqProfile()
{
CreateMap
();
CreateMap()
.ForMember(d => d.PurchaseReqItems, o => o.MapFrom(src => src.PurchaseReqItems))
.ForMember(d => d.PurchaseReqCharges, o => o.MapFrom(src => src.PurchaseReqCharges));
}
}
public class PurchaseReqItemProfile : Profile
{
public PurchaseReqItemProfile()
{
CreateMap();
CreateMap()
.ForMember(d => d.TaxRate, o => o.Ignore());
}
}
public class PurchaseReqChargeProfile : Profile
{
public PurchaseReqChargeProfile()
{
CreateMap();
CreateMap()
.ForMember(d => d.OtherCharge, o => o.Ignore());
}
}
AutoMapper версии 13.0.1
Код: Выделить всё
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());Код: Выделить всё
public async Task
GetPrForUpdateAsync(string prNumber)
{
ArgumentNullException.ThrowIfNull(prNumber);
return await context.PurchaseReqs
.Include(pr => pr.PurchaseReqItems)
.Include(pr => pr.PurchaseReqCharges)
.SingleOrDefaultAsync(pr => pr.PrNumber == prNumber);
}
Код: Выделить всё
[HttpPut]
[CanUserUpdate(RolesConstant.PurchaseReq)]
public async Task UpdatePurchaseReq([FromBody] PurchaseReqDto purchaseReqDto)
{
if (!short.TryParse(User.Claims.FirstOrDefault(c => c.Type == "sub")?.Value,out var userId))
return Unauthorized();
var purchaseReq = await purchaseReqRepo.GetPrForUpdateAsync(purchaseReqDto.PrNumber);
if (purchaseReq == null)
return NotFound();
if (purchaseReq.IsSubmitted)
return BadRequest(string.Format($"PR ({purchaseReq.PrNumber}) was already submitted for approval and can't be updated."));
mapper.Map(purchaseReqDto, purchaseReq, options =>
options.BeforeMap((prDto, pr) =>
{
pr.RemoveUnMatchedItems(prDto.PurchaseReqItems);
pr.RemoveUnMatchedCharges(prDto.PurchaseReqCharges);
}));
purchaseReq.UpdateBy(userId);
await purchaseReqRepo.SaveChangesAsync();
return NoContent();
}
Я не знаю, что я здесь делаю неправильно.
Подробнее здесь: https://stackoverflow.com/questions/783 ... automapper
Мобильная версия