C# 如何使用AutoMapper从多个对象映射单个对象?
资产可用性C# 如何使用AutoMapper从多个对象映射单个对象?,c#,asp.net-core,automapper,C#,Asp.net Core,Automapper,资产可用性 public class AssetAvailabilityDto { public int Id { get; set; } public int AssetId { get; set; } public int CalendarId { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } } 资产 预定时间表 p
public class AssetAvailabilityDto
{
public int Id { get; set; }
public int AssetId { get; set; }
public int CalendarId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
资产
预定时间表
public class BookingScheduleDto
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
目的地是容纳他们所有人
public class AssetInformationDto
{
public int AssetId { get; set; }
public int CategoryId { get; set; }
public string AssetName { get; set; }
public string AssetDescription { get; set; }
public bool AssetIsActive { get; set; }
public int AssetOwnedBy { get; set; }
public DateTime AssetLastModifiedAt { get; set; }
// End of Assets
public int AvailabilityId { get; set; }
public DateTime AvailabilityStartDate { get; set; }
public DateTime AvailabilityEndDate { get; set; }
// End of Availability
public List<DateTime> BookingStartDate { get; set; } = new List<DateTime>();
public List<DateTime> BookingEndDate { get; set; } = new List<DateTime>();
// End of Booking schedule
}
公共类AssetInformationDto
{
公共int AssetId{get;set;}
public int CategoryId{get;set;}
公共字符串AssetName{get;set;}
公共字符串AssetDescription{get;set;}
公共布尔资产活动{get;set;}
公共int AssetOwnedBy{get;set;}
public DateTime AssetLastModifiedAt{get;set;}
//资产终止
public int AvailabilityId{get;set;}
公共日期时间可用性开始日期{get;set;}
公共DateTime可用性EndDate{get;set;}
//可用性结束
public List BookingStartDate{get;set;}=new List();
public List bookingdate{get;set;}=new List();
//预订时间表结束
}
映射发生的位置
AssetInformationDto information = new AssetInformationDto();
AssetDto asset = _assetService.GetAssetById(id);
AssetAvailabilityDto assetAvailability = _assetService.GetAssetAvailabilityByAssetId(id);
IEnumerable<BookingScheduleDto> schedule = _assetService.GetAssetBookingSchedule(id, true);
information = _mapper.Map<AssetInformationDto>(asset);
information = _mapper.Map<AssetInformationDto>(assetAvailability);
information = _mapper.Map<AssetInformationDto>(schedule);
AssetInformationDto information=new AssetInformationDto();
AssetTo asset=\u assetService.GetAssetById(id);
AssetAvailabilityTo assetAvailability=_assetService.GetAssetAvailabilityByAsetId(id);
IEnumerable schedule=\u assetService.GetAssetBookingSchedule(id,true);
information=\u mapper.Map是上述问题的解决方案,通过创建函数“EntityMapper”并稍微修改它以接受注入的\u mapper对象,但我想学习另一种解决方案
简单地配置AutoMapper不就可以做到这一点吗?
也许它不起作用的原因是我写的配置不好
这里的问题是,在每个映射对象之后,(在每个信息=…)以前指定的属性都会恢复为其类型的默认值。这就是为什么我在配置映射方式的相应类中包含了allothermembers.Ignore()
。我一直在寻找的是Mapper.Map(TSource,TDestination)
(在我的例子中,由于依赖项注入,映射器应该是\ Mapper。)
它执行从源对象到现有目标对象的映射
我以前使用的方法(Mapper.Map(TSource)
)执行从源对象到新的目标对象的映射
之前的代码:
AssetInformationDto information = new AssetInformationDto();
AssetDto asset = _assetService.GetAssetById(id);
AssetAvailabilityDto assetAvailability = _assetService.GetAssetAvailabilityByAssetId(id);
IEnumerable<BookingScheduleDto> schedule = _assetService.GetAssetBookingSchedule(id, true);
information = _mapper.Map<AssetInformationDto>(asset);
information = _mapper.Map<AssetInformationDto>(assetAvailability);
information = _mapper.Map<AssetInformationDto>(schedule);
AssetInformationDto information=new AssetInformationDto();
AssetTo asset=\u assetService.GetAssetById(id);
AssetAvailabilityTo assetAvailability=_assetService.GetAssetAvailabilityByAsetId(id);
IEnumerable schedule=\u assetService.GetAssetBookingSchedule(id,true);
信息=_mapper.Map(资产);
信息=_mapper.Map(资产可用性);
信息=_mapper.Map(时间表);
解决我问题的代码
AssetInformationDto information;
AssetDto asset = _assetService.GetAssetById(id);
AssetAvailabilityDto assetAvailability =
_assetService.GetAssetAvailabilityByAssetId(id);
IEnumerable<BookingScheduleDto> schedule =
_assetService.GetAssetBookingSchedule(id, true);
information = _mapper.Map<AssetInformationDto>(asset);
_mapper.Map(assetAvailability, information);
_mapper.Map(schedule, information);
AssetInformationDto信息;
AssetTo asset=\u assetService.GetAssetById(id);
资产可用性与资产可用性之比=
_assetService.GetAssetAvailabilityByAsetId(id);
IEnumerable时间表=
_assetService.GetAssetBookingSchedule(id,true);
信息=_mapper.Map(资产);
_地图(资产可用性、信息);
_地图(时间表、信息);
AssetInformationDto information = new AssetInformationDto();
AssetDto asset = _assetService.GetAssetById(id);
AssetAvailabilityDto assetAvailability = _assetService.GetAssetAvailabilityByAssetId(id);
IEnumerable<BookingScheduleDto> schedule = _assetService.GetAssetBookingSchedule(id, true);
information = _mapper.Map<AssetInformationDto>(asset);
information = _mapper.Map<AssetInformationDto>(assetAvailability);
information = _mapper.Map<AssetInformationDto>(schedule);
AssetInformationDto information;
AssetDto asset = _assetService.GetAssetById(id);
AssetAvailabilityDto assetAvailability =
_assetService.GetAssetAvailabilityByAssetId(id);
IEnumerable<BookingScheduleDto> schedule =
_assetService.GetAssetBookingSchedule(id, true);
information = _mapper.Map<AssetInformationDto>(asset);
_mapper.Map(assetAvailability, information);
_mapper.Map(schedule, information);