我正在尝试将DTO映射到Type。我已经用另一种方式做了。
棘手的部分是正确处理Many-to-Many
关系。
在Book
具有ICollection<BookCategory>
的情况下,BookDTO
具有ICollection<int>
。
namespace Core.Entities
{
public class Book : IAggregateRoot
{
public int BookId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string CoverImageUrl { get; set; }
public string Layout { get; set; }
public ICollection<Chapter> Chapters { get; set; }
public ICollection<BookCategory> BookCategories { get; set; }
public ICollection<BookTag> BookTags { get; set; }
}
}
namespace API.DTOs
{
public class BookDTO : DTO
{
public int BookId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string CoverImageUrl { get; set; }
public string Layout { get; set; }
public ICollection<int> Categories { get; set; }
public ICollection<int> Tags { get; set; }
}
}
namespace Core.Entities
{
public class BookCategory : IAggregateRoot
{
public int BookId { get; set; }
public Book Book { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}
}
在我的MappingProfile中,我有这样的代码:
namespace API
{
public class MappingProfile : Profile
{
public MappingProfile()
{
// Entity -> DTO
// src being the concrete type T in CreateMap<T, DTO>()
CreateMap<Book, BookDTO>()
.ForMember(dto => dto.Categories, options => options.MapFrom(src => src.BookCategories.Select(bc => bc.CategoryId).ToList()))
.ForMember(dto => dto.Tags, options => options.MapFrom(src => src.BookTags.Select(bt => bt.TagId).ToList()));
...
// DTO -> Enitity
CreateMap<BookDTO, Book>()
.ForMember(
book => book.BookCategories,
options => options.ConvertUsing(dto => dto.Categories.Select(category => new BookCategory
{
BookId = dto.BookId,
CategoryId = category
}).ToList()));
...
// The simplified version
CreateMap<List<int>, List<BookCategory>>().ConstructUsing(listOfInts => listOfInts.Select(singleInt => new BookCategory {
BookId = singleInt,
CategoryId = singleInt
}).ToList());
}
// An external conversion function
// Usage:
// ConvertUsing(dto => ConvertBookDtoToList(dto))
private ICollection<BookCategory> ConvertBookDtoToList(BookDTO dto)
{
return dto.Categories.Select(category => new BookCategory {
BookId = dto.BookId,
CategoryId = category
}).ToList();
}
}
}
然而,使用这些方法中的任何一种都会导致错误:
Severity Code Description Project File Line Suppression State
Error CS0411 The type arguments for method 'IMemberConfigurationExpression<BookDTO, Book, object>.ConvertUsing<TValueConverter, TSourceMember>(Expression<Func<BookDTO, TSourceMember>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. API C:Userspebessourcereposip6_uiux-ereader-appbook_designerAPIMappingProfile.cs 40 Active
我真的不理解错误信息,这在我看来是有道理的。
我该如何解决此问题?
CovertUsing用于其他目的,并且必须在CreateMap之后。它用来设置另一种方式将一个值转换为另一个
你必须使用MapFrom而不是ConvertUsing,它可以做你想做的事:
CreateMap<BookDTO, Book>()
.ForMember(
book => book.BookCategories,
options => options.MapFrom(dto =>
dto.Categories.Select(category => new BookCategory
{
BookId = dto.BookId,
CategoryId = category
}).ToList()
)
);