[英]Map ICollection With AutoMapper
大家好,我在使用 AutoMapper 映射某些模型時遇到了很多麻煩,我想知道您是否可以為我指明正確的方向。
我有一些實體如下;
public class Camp
{
public int CampId { get; set; }
public string Name { get; set; }
public string Moniker { get; set; }
public Location Location { get; set; }
public DateTime EventDate { get; set; } = DateTime.MinValue;
public int Length { get; set; } = 1;
public ICollection<Talk> Talks { get; set; }
}
public class Talk
{
public int TalkId { get; set; }
public Camp Camp { get; set; }
public string Title { get; set; }
public string Abstract { get; set; }
public int Level { get; set; }
public Speaker Speaker { get; set; }
}
以及相應的 DTO
public class CampModel
{
public string Name { get; set; }
public string Moniker { get; set; }
public DateTime EventDate { get; set; } = DateTime.MinValue;
public int Length { get; set; } = 1;
public string Venue { get; set; }
public string LocationAddress1 { get; set; }
public string LocationAddress2 { get; set; }
public string LocationAddress3 { get; set; }
public string LocationCityTown { get; set; }
public string LocationStateProvince { get; set; }
public string LocationPostalCode { get; set; }
public string LocationCountry { get; set; }
public ICollection<TalkModel> Talks { get; set; }
}
public class TalkModel
{
public int TalkId { get; set; }
public string Title { get; set; }
public string Abstract { get; set; }
public int Level { get; set; }
}
我想在我的 controller 上使用自動映射器,如下所示:
[Route("api/[controller]")]
public class CampsController : ControllerBase
{
private readonly ICampRepository _repository;
private readonly IMapper _mapper;
public CampsController(ICampRepository repository, IMapper mapper)
{
_repository = repository;
_mapper = mapper;
}
[HttpGet]
public async Task<ActionResult<CampModel[]>> Get(bool includeTalks = false)
{
try
{
var camps = await _repository.GetAllCampsAsync(includeTalks);
var mapper = _mapper.Map<CampModel[]>(camps);
return mapper;
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, "Database failure" + " Message: " + e);
}
}
}
我正在像這樣返回我的存儲庫中的營地:
public async Task<Camp[]> GetAllCampsByEventDate(DateTime dateTime, bool includeTalks = false)
{
_logger.LogInformation($"Getting all Camps");
IQueryable<Camp> query = _context.Camps
.Include(c => c.Location);
if (includeTalks)
{
query = query
.Include(c => c.Talks)
.ThenInclude(t => t.Speaker);
}
// Order It
query = query.OrderByDescending(c => c.EventDate)
.Where(c => c.EventDate.Date == dateTime.Date);
return await query.ToArrayAsync();
}
我已經在 Startup.Cs 上注冊了我的自動映射器
services.AddAutoMapper(typeof(CampProfile).Assembly);
使用這樣的配置文件:
public class CampProfile : Profile
{
public CampProfile()
{
this.CreateMap<Camp, CampModel>()
.ForMember(c => c.Venue, o => o.MapFrom(m => m.Location.VenueName))
.ForMember(c => c.Talks, o => o.MapFrom(m => m.Talks))
.ReverseMap();
}
}
但是當我嘗試達到我的端點時,我收到以下錯誤:
Message: AutoMapper.AutoMapperMappingException: Error mapping types.
Mapping types:
Object -> CampModel[]
System.Object -> CoreCodeCamp.Models.CampModel[]
---> AutoMapper.AutoMapperMappingException: Error mapping types.
Mapping types:
Camp -> CampModel
CoreCodeCamp.Data.Camp -> CoreCodeCamp.Models.CampModel
Type Map configuration:
Camp -> CampModel
CoreCodeCamp.Data.Camp -> CoreCodeCamp.Models.CampModel
Destination Member:
Talks
---> AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
Talk -> TalkModel
CoreCodeCamp.Data.Talk -> CoreCodeCamp.Models.TalkModel
我究竟做錯了什么? 我認為這個問題與public ICollection<Talk> Talks { get; set; }
public ICollection<Talk> Talks { get; set; }
public ICollection<Talk> Talks { get; set; }
財產。 提前致謝
只需在Talk
和TalkModel
之間添加映射器,如下所示:
public class CampProfile : Profile
{
public CampProfile()
{
this.CreateMap<Talk, TalkModel>();
this.CreateMap<Camp, CampModel>()
.ForMember(c => c.Venue, o => o.MapFrom(m => m.Location.VenueName))
//.ForMember(c => c.Talks, o => o.MapFrom(m => m.Talks))
.ReverseMap();
}
}
根據上面給出的代碼,您需要在Talk
和TalkModel
,查看 AutoMapper 中的嵌套映射。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.