[英]Using Automapper - how do I map from this ICollection?
I am using entity framework and automapper for the first time.我第一次使用实体框架和自动映射器。 I am lost.
我搞不清楚了。 I have a class containing an
ICollection
of another class which I wish to map:我有一个包含我希望映射的另一个类的
ICollection
的类:
Here are my classes:这是我的课程:
public class BaseEntity {
public int Id { get; set; }
}
public class Car : BaseEntity {
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public ICollection<CarPart> CarParts { get; set; }
}
public class Part : BaseEntity {
public string PartName { get; set; }
}
public class CarPart : BaseEntity {
public int CarId { get; set; }
public Car Car { get; set; }
public int PartId { get; set; }
public Part Part { get; set; }
}
and then my DTO:然后是我的 DTO:
public class CarDTO {
public int Id { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public ICollection<CarPart> CarParts { get; set; }
}
and my mapping configuration profile contains:我的映射配置文件包含:
CreateMap<Car, CarDTO> ();
and then in my controller I use:然后在我的控制器中我使用:
private readonly IMapper _mapper;
return _mapper.Map<Car, CarDTO>(car);
When I hit the API I get the following error:当我点击 API 时,我收到以下错误:
Destination Member:
CarParts
---> AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
CarPart -> Part
Entities.CarPart -> Entities.Part
What changes do I need to make to fix this?我需要做哪些改变来解决这个问题? Thank-you.
谢谢。
Edit: The following is all in StartUp.cs:编辑:以下全部在 StartUp.cs 中:
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(MappingProfiles));
}
Edit #2: Changing the scope of this a little bit.编辑#2:稍微改变这个范围。 I changed CarDTO to
我将 CarDTO 更改为
public class CarDTO {
public int CarId { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public IReadOnlyList<Part> Parts { get; set; }
}
and added the following DTO:并添加了以下 DTO:
public class PartDTO
{
public int PartId { get; set; }
public string PartName { get; set; }
}
and edited my mapping configuration profile to并将我的映射配置文件编辑为
public MappingProfiles () {
CreateMap<Car, CarDTO> ()
.ForMember(dest => dest.CarId, opt => opt.MapFrom(src => src.Id))
.ForMember (dest => dest.Parts, opt =>
opt.MapFrom (src => src.CarParts.Select (x => x.Part
)));
CreateMap<Part, PartDTO>()
.ForMember(dest => dest.PartId, opt => opt.MapFrom(src => src.Id))
}
which returns this:返回这个:
{
"carId": 8,
"make": "some make",
"model": "some model",
"year": 2001,
"parts": [
{
"partName": "Fully sick Wheels",
"id": 3
},
{
"partName": "Mad Engine",
"id": 4
}
]
}
How can I change this to:我该如何将其更改为:
{
"carId": 8,
"make": "some make",
"model": "some model",
"year": 2001,
"parts": [
{
"partName": "Fully sick Wheels",
"partId": 3
},
{
"partName": "Mad Engine",
"partId": 4
}
]
}
Change the collection property in your CarDTO
so that it becomes -更改
CarDTO
的集合属性,使其变为 -
public class CarDTO
{
public int CarId { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public IReadOnlyList<PartDTO> Parts { get; set; } // this one
}
Just change it from IReadOnlyList<Part>
to IReadOnlyList<PartDTO>
That should solve your problem.只需将它从
IReadOnlyList<Part>
更改为IReadOnlyList<PartDTO>
那应该可以解决您的问题。
With your current definition of CarDTO
, the following configuration in your first map -根据您当前对
CarDTO
定义,您的第一张地图中的以下配置 -
.ForMember (dest => dest.Parts, opt =>
opt.MapFrom (src => src.CarParts.Select (x => x.Part)));
is basically mapping from Part
to Part
.基本上是从
Part
映射到Part
。
So your second map, from Part
to PartDTO
, is not being used at all.所以你的第二张地图,从
Part
到PartDTO
,根本没有被使用。 Hence PartId
is not showing up.因此
PartId
没有出现。
那应该工作。
CreateMap<Car, CarDTO>().ReverseMap();
Add also也添加
CreateMap<CarPart, CarPart> ();
CreateMap<Part, Part> ();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.