0

给定以下代码,是否有让 Automapper 执行此操作而不是手动设置属性?我试图避免在 Mapper.CreateMap 上进行一对一的映射,因为我不断地在 Contract 中添加和删除字段,并且此刻必须记住使用更改来更新此方法。

private void MapModelToContract(ContractDto model, Contract contract)
        {
            contract.Id = model.Id;
            contract.ContractDetail.BuyerCornhouseName = model.ContractDetailBuyerCornhouseName;
            contract.ContractDetail.CnoCornLtdToBuyer = model.ContractDetailCnoCornLtdToBuyer;
            contract.ContractDetail.CnoCornPartToCornLtd = model.ContractDetailCnoCornPartToCornLtd;
            contract.ContractDetail.CnoSellerCornLtd = model.ContractDetailCnoSellerCornLtd;
            contract.ContractDetail.CnoSellerCornPart = model.ContractDetailCnoSellerCornPart;
            contract.ContractDetail.Commission = model.ContractDetailCommission;
            contract.ContractDetail.ContractPrice = model.ContractDetailContractPrice;
            contract.ContractDetail.SalesPriceToCornLtd = model.ContractDetailSalesPriceToCornLtd;
            contract.ContractDetail.ContractedQuantity = model.ContractDetailContractedQuantity;
            contract.ContractDetail.CostWarehouseToFob = model.ContractDetailCostWarehouseToFob;
            contract.ContractDetail.Date = model.ContractDetailDate;
            contract.ContractDetail.DeliveryDate = model.ContractDetailDeliveryDate;
            contract.ContractDetail.FinalBuyerName = model.ContractDetailFinalBuyerName;
            contract.ContractDetail.FinalSalesPrice = model.ContractDetailFinalSalesPrice;
            contract.ContractDetail.Grade = model.ContractDetailGrade;
            contract.ContractDetail.Notes = model.ContractDetailNotes;
            contract.ContractDetail.PayableQuantity = model.ContractDetailPayableQuantity;
            contract.ContractDetail.SalesShipmentPeriod = model.ContractDetailSalesShipmentPeriod;
            contract.ContractDetail.SellerName = model.ContractDetailSellerName;
            contract.ContractDetail.ToBePaidBy = model.ContractDetailToBePaidBy;
            contract.ContractDetail.SalesBasis = model.ContractDetailSalesBasis;
            contract.ContractDetail.SalesFreightValue = model.ContractDetailSalesFreightValue;

            contract.CurrencyContract.Date = model.CurrencyContractDate;
            contract.CurrencyContract.ExchangeRate = model.CurrencyContractExchangeRate;
            contract.CurrencyContract.Number = model.CurrencyContractNumber;
            contract.CurrencyContract.Value = model.CurrencyContractValue;
            contract.CurrencyContract.IsAcc = model.CurrencyContractIsAcc;
            contract.CurrencyContract.RepaidOn = model.CurrencyContractRepaidOn;
            contract.CurrencyContract.RepaymentDate = model.CurrencyContractRepaymentDate;

            contract.Discounts.Reason = model.DiscountsReason;
            contract.Discounts.Value = model.DiscountsValue;

            contract.DocumentControl.GuaranteeLetterReceived = model.DocumentControlGuaranteeLetterReceived;
            contract.DocumentControl.PhotosReceived = model.DocumentControlPhotosReceived;

            contract.GoodsStatus.ContainerNumber = model.GoodsStatusContainerNumber;
            contract.GoodsStatus.EtaPort = model.GoodsStatusEtaPort;
            contract.GoodsStatus.ExpectedDeliveryDate = model.GoodsStatusExpectedDeliveryDate;
            contract.GoodsStatus.Port = model.GoodsStatusPort;
            contract.GoodsStatus.ShippedStatus = model.GoodsStatusShippedStatus;
            contract.GoodsStatus.VesselDeadline = model.GoodsStatusVesselDeadline;
            contract.GoodsStatus.VesselName = model.GoodsStatusVesselName;
            contract.GoodsStatus.HasSellerConfirmedRelease = model.GoodsStatusHasSellerConfirmedRelease;
            contract.GoodsStatus.ReceivedShippingInstructions = model.GoodsStatusReceivedShippingInstructions;
            contract.GoodsStatus.Notes = model.GoodsStatusNotes;

            contract.NotaFiscalCornPart.Date = model.NotaFiscalCornPartDate;
            contract.NotaFiscalCornPart.ExchangeRate = model.NotaFiscalCornPartExchangeRate;
            contract.NotaFiscalCornPart.Number = model.NotaFiscalCornPartNumber;

            contract.NotaFiscalSeller.Date = model.NotaFiscalSellerDate;
            contract.NotaFiscalSeller.GoodsReleased = model.NotaFiscalSellerGoodsReleased;
            contract.NotaFiscalSeller.Number = model.NotaFiscalSellerNumber;

            contract.TransferToSeller.Date = model.TransferToSellerDate;
            contract.TransferToSeller.TransferredFrom = model.ContractDetailBuyerCornhouseName;
            contract.TransferToSeller.Value = model.TransferToSellerValue;

            contract.CommissionLawyer.IsPaid = model.CommissionLawyerIsPaid;
        }
4

1 回答 1

1

下面的映射器适合你。

如果 Source ( ContractDto) 和 Destination ( Contract) 具有相同的属性名称。

Mapper.CreateMap<ContractDto, Contract>(); // Maps if ContractDto & Contract have same property names

如果 Source ( ContractDto) 和 Destination ( Contract) 具有不同的属性名称。然后

Mapper.CreateMap<ContractDto, Contract>()
    .ForMember(destination => destination.BuyerCornhouseName, options => options.MapFrom(source => source.ContractDetailBuyerCornhouseName))
    .ForMember(destination => destination.CnoCornLtdToBuyer, options => options.MapFrom(source => source.ContractDetailCnoCornLtdToBuyer));

    // Explicitly you have specify for other properties also
于 2012-11-13T06:40:00.760 回答