gpt4 book ai didi

c# - AutoMapper 不映射子属性

转载 作者:行者123 更新时间:2023-11-30 16:44:36 24 4
gpt4 key购买 nike

我收到 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; }

当 Dependents 被移除时 Subscriber 映射完美

至于Dependent 实体和Model,它们与所有项目public 一起复制和粘贴。看起来好像 AutoMapper 正试图将集合压缩到 int 而不是指定的 map 中。

有什么想法吗?

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);
}

现在返回的错误是类型 X 中的方法 'Add' 没有实现'

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.

将依赖项的自动映射器配置更改为

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,但它仍然可以进行单元测试,这是一个很大的警告,但它仍然会在功能测试中中断

我想不出还有什么要添加的,因为 Dependents 甚至没有整数字段

附录:实体、Dto 和调用者都在不同的库中,但我无法在 .NETFiddle 中复制它

最佳答案

Automapper 通常应该为您处理集合。它还将查找可能适用的任何已知 map ,因此无需在 MapFrom 中调用 Mapper.Map

只需为单个实体定义映射:

CreateMap<Dependent, DependentDetailDto>();
CreateMap<Member, MemberDetailDto>()
.ForMember(dto => dto.Dependents, o => o.MapFrom(src => src.Dependents);

关于c# - AutoMapper 不映射子属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43257247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com