我一直试图解决这个问题一天,并没有在哪里,所以我希望有人可能已经解决了这个问题.我发现最接近解决方案的是如何使用AutoMapper简单地将NHibernate ISet映射到IList并通过AutoMapper 将IList 映射到ICollection,但仍然没有乐趣.
我有一个看起来像这样的数据对象:
public class Parent
{
public virtual ISet<Child> Children {get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和一个看起来像这样的业务对象:
public class ParentDto
{
public IList<ChildDto> Children {get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用AutoMapper从Data to Business映射工作正常:
...
Mapper.CreateMap<Parent, ParentDto>();
Mapper.CreateMap<Child, ChildDto>();
...
ParentDto destination = CWMapper.Map<Parent, ParentDto>(source);
Run Code Online (Sandbox Code Playgroud)
但是,当我从业务映射到数据时,我得到错误:
...
Mapper.CreateMap<ParentDto, Parent>();
Mapper.CreateMap<ChildDto, Child>();
...
Parent destination = CWMapper.Map<ParentDto, Parent>(source);
Run Code Online (Sandbox Code Playgroud)
无法将'System.Collections.Generic.List'类型的对象强制转换为''Iesi.Collections.Generic.ISet'
我添加了一个自定义映射:
Mapper.CreateMap<ParentDto, Parent>()
.ForMember(m => m.Children, o => o.MapFrom(s => ToISet<ChildDto>(s.Children)));
private static ISet<T> ToISet<T>(IEnumerable<T> list)
{
Iesi.Collections.Generic.ISet<T> set = …Run Code Online (Sandbox Code Playgroud)