AutoMapper - 试图将我的对象 属性 展平为字符串但出现错误
AutoMapper - Trying to flatten my object property to string but getting an error
我正在尝试将我的 ProductDto
映射到 Product
class。将 ProductDto
class 中的字符串 属性 映射到 Product
class 中的 Brand
属性 时出现错误。 =28=]
AutoMapper.AutoMapperMappingException: Error mapping types.
Mapping types:
ProductDto -> Product
SilksyAPI.Dto.ProductDto -> SilksyAPI.Entities.Product
Type Map configuration:
ProductDto -> Product
SilksyAPI.Dto.ProductDto -> SilksyAPI.Entities.Product
Destination Member:
Brand
---> AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
String -> Brand
System.String -> SilksyAPI.Entities.Brand
这是我的映射配置文件:
CreateMap<Product, ProductDto>()
.ForMember(dest => dest.Brand, opt => opt.MapFrom(p => p.Brand.Name))
.ForMember(dest => dest.Categories, opt => opt.MapFrom(p => p.ProductCategories.Select(pc => pc.Category.Name)));
CreateMap<ProductDto, Product>()
.ForMember(dest => dest.Brand, opt => opt.MapFrom(src => src.Name));
Class是:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int BrandId { get; set; }
public Brand Brand { get; set; }
public ICollection<ProductCategory> ProductCategories { get; set; }
}
public class Brand
{
public int Id { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
}
Dto Class
public class ProductDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Brand { get; set; }
public List<string> Categories { get; set; }
}
我尝试了不同的映射配置文件,但我无法从 ProductDto
映射到 Product
,但可以毫无问题地从 Product
映射到 ProductDto
。
这是我尝试过的其他一些东西
CreateMap<Brand, string>()
.IncludeMembers(b => b.Name);
CreateMap<Product, ProductDto>()
.ForMember(dest => dest.Brand, opt => opt.MapFrom(p => p.Brand.Name))
.ForMember(dest => dest.Categories, opt => opt.MapFrom(p => p.ProductCategories.Select(pc => pc.Category.Name)))
.ReverseMap();
和
Mapper.CreateMap<Brand, string>().ConvertUsing(source => source.Name ?? string.Empty);
从您的第一个映射配置文件,当从 ProductDto
映射到 Product
时,对于 Brand
属性,您需要创建一个新的 Brand
实例如下:
cfg.CreateMap<ProductDto, Product>()
.ForMember(dest => dest.Brand, opt => opt.MapFrom(src => new Brand { Name = src.Name }));
For .IncludeMembers()
我认为它不适合您的情况,因为您要从 object
映射到 string
,而不是 object
到 object
。
For .ConvertUsing()
您需要添加以下代码以将品牌 属性 从 string
映射到 Brand
。
cfg.CreateMap<string, Brand>()
.ConvertUsing(source => new Brand { Name = source });
我正在尝试将我的 ProductDto
映射到 Product
class。将 ProductDto
class 中的字符串 属性 映射到 Product
class 中的 Brand
属性 时出现错误。 =28=]
AutoMapper.AutoMapperMappingException: Error mapping types.
Mapping types:
ProductDto -> Product
SilksyAPI.Dto.ProductDto -> SilksyAPI.Entities.Product
Type Map configuration:
ProductDto -> Product
SilksyAPI.Dto.ProductDto -> SilksyAPI.Entities.Product
Destination Member:
Brand
---> AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
String -> Brand
System.String -> SilksyAPI.Entities.Brand
这是我的映射配置文件:
CreateMap<Product, ProductDto>()
.ForMember(dest => dest.Brand, opt => opt.MapFrom(p => p.Brand.Name))
.ForMember(dest => dest.Categories, opt => opt.MapFrom(p => p.ProductCategories.Select(pc => pc.Category.Name)));
CreateMap<ProductDto, Product>()
.ForMember(dest => dest.Brand, opt => opt.MapFrom(src => src.Name));
Class是:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int BrandId { get; set; }
public Brand Brand { get; set; }
public ICollection<ProductCategory> ProductCategories { get; set; }
}
public class Brand
{
public int Id { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
}
Dto Class
public class ProductDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Brand { get; set; }
public List<string> Categories { get; set; }
}
我尝试了不同的映射配置文件,但我无法从 ProductDto
映射到 Product
,但可以毫无问题地从 Product
映射到 ProductDto
。
这是我尝试过的其他一些东西
CreateMap<Brand, string>()
.IncludeMembers(b => b.Name);
CreateMap<Product, ProductDto>()
.ForMember(dest => dest.Brand, opt => opt.MapFrom(p => p.Brand.Name))
.ForMember(dest => dest.Categories, opt => opt.MapFrom(p => p.ProductCategories.Select(pc => pc.Category.Name)))
.ReverseMap();
和
Mapper.CreateMap<Brand, string>().ConvertUsing(source => source.Name ?? string.Empty);
从您的第一个映射配置文件,当从 ProductDto
映射到 Product
时,对于 Brand
属性,您需要创建一个新的 Brand
实例如下:
cfg.CreateMap<ProductDto, Product>()
.ForMember(dest => dest.Brand, opt => opt.MapFrom(src => new Brand { Name = src.Name }));
For
.IncludeMembers()
我认为它不适合您的情况,因为您要从 object
映射到 string
,而不是 object
到 object
。
For
.ConvertUsing()
您需要添加以下代码以将品牌 属性 从 string
映射到 Brand
。
cfg.CreateMap<string, Brand>()
.ConvertUsing(source => new Brand { Name = source });