[英]Automapper AutoMapper.AutoMapperMappingException: Error mapping types
I am getting a peculiar error with Automapper 我在Automapper中遇到了一个特殊的错误
Error messages: 错误讯息:
Mapping types:
TransportOffer -> TransportOfferDto
Model.TransportOffer -> Dto.TransportOfferDto
Type Map configuration:
TransportOffer -> TransportOfferDto
Model.TransportOffer -> Dto.TransportOfferDto
Property:
FromCity ---> AutoMapper.AutoMapperMappingException: Error mapping types.
Mapping types:
City -> CityDto
Model.City -> Dto.CityDto
Type Map configuration:
City -> CityDto
Model.City -> Dto.CityDto
Property:
Country ---> System.TypeLoadException: Method 'Add' in type 'Proxy_System.Collections.Generic.ICollection`1[[Dto.CountryDto, BusinessLogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]__17474517' from assembly 'AutoMapper.Proxies, Version=0.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005' does not have an implementation.
Below are my entities and DTO and the query that retrieves the data. 以下是我的实体,DTO和检索数据的查询。 I am using Automapper 7.0.1 and Automapper.Attributes 6.0.1 我正在使用Automapper 7.0.1和Automapper.Attributes 6.0.1
I have tried also with custom mapping configuration and the error is the same. 我也尝试过使用自定义映射配置,并且错误相同。
Here is the Automapper custom property configuration: 这是Automapper定制属性配置:
Mapper.Initialize
(
config =>
{
config.CreateMap<TransportOfferDto, TransportOffer>()
.ForMember(dest => dest.FromCity, conf => conf.MapFrom(src => src.FromCity))
.ForMember(dest => dest.FromCountry, conf => conf.MapFrom(src => src.FromCountry))
.ForMember(dest => dest.ToCity, conf => conf.MapFrom(src => src.ToCity))
.ForMember(dest => dest.ToCountry, conf => conf.MapFrom(src => src.ToCountry));
}
);
Country - entity: 国家-实体:
public class Country : BaseEntity<Int32>
{
public string Name { get; set; }
public string Code { get; set; }
public string Capital { get; set; }
public Int32 TotalSold { get; set; }
}
CountryDto CountryDto
[MapsTo(typeof(Country))]
[MapsFrom(typeof(Country))]
public class CountryDto : EntityDto<Int32>
{
public string Name { get; set; }
public string Code { get; set; }
public string Capital { get; set; }
public Int32 TotalSold { get; set; }
}
City - entity: 城市-实体:
public class City : BaseEntity<Int32>
{
public string Name { get; set; }
public Int32 CountryID { get; set; }
public virtual Country Country { get; set; }
}
CityDto CityDto
[MapsTo(typeof(City))]
[MapsFrom(typeof(City))]
public class CityDto : EntityDto<Int32>
{
public Int32 CountryID { get; set; }
public virtual ICollection<CountryDto> Country { get; set; }
public string Name { get; set; }
}
TransportOffer - entity: TransportOffer-实体:
public class TransportOffer : BaseEntity<Guid>
{
public Guid TransportToken { get; set; }
public DateTime Date { get; set; }
public Int32 FromCityID { get; set; }
public virtual City FromCity { get; set; }
public Int32 FromCountryID { get; set; }
public virtual Country FromCountry { get; set; }
public Int32 ToCityID { get; set; }
public virtual City ToCity { get; set; }
public Int32 ToCountryID { get; set; }
public virtual Country ToCountry { get; set; }
}
TransportOfferDto: TransportOfferDto:
[MapsTo(typeof(TransportOffer))]
[MapsFrom(typeof(TransportOffer))]
public class TransportOfferDto : EntityDto<Guid>
{
public Guid TransportToken { get; set; }
public DateTime Date { get; set; }
public Int32 FromCityID { get; set; }
public virtual CityDto FromCity { get; set; }
public Int32 FromCountryID { get; set; }
public virtual CountryDto FromCountry { get; set; }
public Int32 ToCityID { get; set; }
public virtual CityDto ToCity { get; set; }
public Int32 ToCountryID { get; set; }
public virtual Country ToCountry { get; set; }
}
Query 询问
var query = Repository.GetAll()
.Include(x => x.FromCountry)
.Include(x => x.FromCity)
.Include(x => x.ToCountry)
.Include(x => x.ToCity)
.Where(p => p.MembershipID == input).ToList();
return ObjectMapper.Map<List<TransportOfferDto>>(query);
Your CityDto class has a collection of Countries 您的CityDto类具有国家/地区集合
[MapsTo(typeof(City))] [MapsFrom(typeof(City))]
public class CityDto : EntityDto<Int32>
{
public Int32 CountryID { get; set; }
public virtual ICollection<CountryDto> Country { get; set; }
public string Name { get; set; }
}
But your City class has a single Country. 但是您的城市班级只有一个国家/地区。
public class City : BaseEntity<Int32>
{
public string Name { get; set; }
public Int32 CountryID { get; set; }
public virtual Country Country { get; set; }
}
How are you mapping them? 您如何映射它们?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.