[英]Map to a collection from another collection with automapper
我的AutoMapper配置看起來像這樣
public static class AutoMapperConfig
{
public static void ConfigureAutoMapper(this IMapperConfigurationExpression cfg)
{
cfg.CreateMap<Foo, BarDTO>()
.ForMember(dto => dto.Genres, o => o.ResolveUsing(b =>
{
var retVal = new List<string>();
foreach (var genre in b.Genres)
{
retVal.Add(genre.Genre.GenreName);
}
return retVal;
}));
}
}
BarDTO的屬性Genre是字符串類型的集合
最終導致異常:
System.InvalidOperationException:'類型'System.Linq.Enumerable'上的通用方法'ToList'與所提供的類型實參和實參兼容。 如果方法是非泛型的,則不應該提供任何類型參數。 “
如果我嘗試這樣做:
cfg.CreateMap<Foo, BarDTO>()
.ForMember(dto => dto.Genres, conf => conf.MapFrom
(
ol => ol.Genres.Select(tr => tr.Genre.GenreName).ToList())
);
這不會引發任何異常,但是什么也不會發生。 好像是無限映射(加載)。
先感謝您!
編輯:
這是我的課。 我使用Entity-Framework Core 2.0,這就是為什么我必須自己使用Foo2Genre創建n對m關系。
public class BarDTO
{
public BarDTO()
{
Genres = new List<string>();
}
public ICollection<string> Genres { get; set; }
}
public class Foo
{
public Foo()
{
Genres = new List<Foo2Genre>();
}
public ICollection<Foo2Genre> Genres { get; set; }
}
public class Foo2Genre
{
public int FooId{ get; set; }
public Foo Foo{ get; set; }
public int GenreId { get; set; }
public Genre Genre { get; set; }
}
public class Genre
{
public Genre()
{
Foos = new List<Foo2Genre>();
}
public string GenreName { get; set; }
public ICollection<Foo2Genre> Foos{ get; set; }
}
我認為您需要包括流派。 這將與EF6中的一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.