0
我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的属性类型为字符串类型的集合
这在异常结束:
System.InvalidOperationException:'没有泛型方法'ToList'类型 'System.Linq.Enumerable'与提供的类型 兼容发言:。如果 方法是非通用的,则不应提供类型参数。 “
如果我试试这个来代替:
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关系。
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; }
}
你可以发布你的'Foo'和'BarDTO'类吗? –
是的,发布一切。也删除ToList,最有可能不需要。 –