我收到了一个关于automapper v6.0.2的问题,其中automapper试图将ICollection
推送到int
而不是指定的集合。
错误:
Mapping types:
Member_B3B146AB4F61AEDE3DF3639BDBD65BFA5BCA0FC414F11960DC39DA834F2A8CBD -> ICollection`1
System.Data.Entity.DynamicProxies.Memberr_B3B146AB4F61AEDE3DF3639BDBD65BFA5BCA0FC414F11960DC39DA834F2A8CBD -> System.Collections.Generic.ICollection`1[[MemberService.Dto.DependentDetailDto, MemberService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Destination path:
MemberDetailDto.Dependents.Dependents
Source value:
System.Data.Entity.DynamicProxies.MbmRosterSubscriber_B3B146AB4F61AEDE3DF3639BDBD65BFA5BCA0FC414F11960DC39DA834F2A8CBD ---> System.OverflowException: Value was either too large or too small for an Int32.
会员
public Member ()
{
this.Dependents = new HashSet<Dependent>();
}
public virtual ICollection<Dependent> Dependents { get; set; }
AutoMapper配置
CreateMap<Dependent, DependentDetailDto>();
CreateMap<Member, MemberDetailDto>()
.ForMember(a => a.Dependents, o => o.MapFrom(x => Mapper.Map<ICollection<Dependent>, ICollection<DependentDetail>>(x.Dependents)));
从属:
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public System.DateTime Dob { get; set; }
public string Sex { get; set; }
public string Relationship { get; set; }
public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
public string DepartmentCode { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string MailCountry { get; set; }
public string Zip { get; set; }
public Nullable<System.DateTime> SsnEncryptDate { get; set; }
删除家属时,订阅者将完美地映射
至于Dependent
实体和Model
,它们会复制并粘贴所有项目public
。看起来好像AutoMapper试图将集合压缩到int
而不是指定的地图。
任何想法?
Dependents
是导航属性
编辑: 这是在异步方法的功能测试中测试的。 已经创建了快速单元测试:
[TestMethod]
public void RetrieveActiveMember()
{
MemberEntity.Member Subscriber = new Member();
ICollection<Dependent> Dependent = new List<Dependent>();
Dependent.Add(new Dependent { HostCountry = "HOME" });
Subscriber.Dependents = Dependent;
var Mapped = AutoMapper.Mapper.Map<Member, MemberDetailDto>(Subscriber);
Assert.IsNotNull(Mapped);
}
现在返回的错误是方法&#39;添加&#39;在类型X中没有实现&#39;
Property:
Dependents ---> System.TypeLoadException: Method 'Add' in type 'Proxy<System.Collections.Generic.ICollection`1[[MemberService.Dto.DependentDetailDto_MemberService_Version=1.0.0.0_Culture=neutral_PublicKeyToken=null]]_mscorlib_Version=4.0.0.0_Culture=neutral_PublicKeyToken=b77a5c561934e089>' from assembly 'AutoMapper.Proxies, Version=0.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005' does not have an implementation.
将依赖项的automapper配置更改为
CreateMap<ICollection<Dependent>, ICollection<DependentDetailDto>>()
.ConstructUsing((ICollection<Dependent> Dependents) => {
List<DependentDetailDto> DependentList = new List<DependentDetailDto>();
foreach (var Dependent in Dependents)
DependentList.Add(new DependentDetailDto { });
return DependentList;
});
使单元测试工作虽然不再实际使用Automapper这是一个很大的警告,但它仍然在功能测试中中断
我无法想到要添加的任何内容,因为家属甚至没有整数字段
附录:实体,Dto和调用者都在不同的库中,但我无法在.NETFiddle中复制它
答案 0 :(得分:2)
Automapper should normally handle collections for you. It will also look up any known maps that might apply, so no need to call Mapper.Map
inside a MapFrom
.
Just define the maps for individual entities:
CreateMap<Dependent, DependentDetailDto>();
CreateMap<Member, MemberDetailDto>()
.ForMember(dto => dto.Dependents, o => o.MapFrom(src => src.Dependents);
答案 1 :(得分:0)
没有找出问题的根本原因,但找到了解决办法。
将MemberDto
属性Dependents
名称更改为Dependentss
并且有效。改变它会再次打破它。我应该补充说,这两个对象都没有其他Dependents属性。
我不知道,但这个名字似乎正在破坏它。