0

我有业务实体对象,我需要使用 AutoMapper 将其映射到 WCF 服务的数据合同对象,但在“MemberDataList”处出现错误我有来自数据访问层的相同版本的数据实体类“成员”,用于映射到业务对象类及其工作正常,不知道为什么这个有问题

源类

 public class Member 
{
    public int MemberID { get; set; }

    public string Title { get; set; }

    public string Surname { get; set; }

    public string Forename { get; set; }

    public string MiddleName { get; set; }

    public string PrevSurname { get; set; }

    public System.DateTime DOB { get; set; }

    public string Sex { get; set; }

    public Address Address { get; set; }

    public ContactDetail ContactDetail { get; set; }

    public Membership Membership { get; set; }

}

目的地等级

[DataContract]
public class MemberData : IIdentifiableEntity
{
    [DataMember]
    public int MemberID { get; set; }

    [DataMember]
    public string Title { get; set; }

    [DataMember]
    public string Surname { get; set; }

    [DataMember]
    public string Forename { get; set; }

    [DataMember]
    public string MiddleName { get; set; }

    [DataMember]
    public string PrevSurname { get; set; }

    [DataMember]
    public System.DateTime DOB { get; set; }

    [DataMember]
    public string Sex { get; set; }

    [DataMember]
    public AddressData Address { get; set; }

    [DataMember]
    public ContactDetailData ContactDetail { get; set; }

    [DataMember]
    public MembershipData Membership { get; set; }

    int IIdentifiableEntity.EntityId
    {
      get { return MemberID; }
      set { MemberID = value; }
    }
}

映射类

public IList<MemberData> GetAllMember()
    {
        IList<MemberData> MemberDataList = null;

        IList<Member> memberList = _MembershipCoreServices.GetAllMember();

        if (memberList != null)
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<Member, MemberData>();
            });

            IMapper mapper = config.CreateMapper();

            MemberDataList = mapper.Map<IList<MemberData>>(memberList).ToList();
        }

        return MemberDataList;
    }

错误

AutoMapper.AutoMapperMappingException was unhandled
HResult=-2146233088
Message=Missing type map configuration or unsupported mapping.

Mapping types:
Address -> AddressData
App.Core.Entities.Address -> App.Services.Contracts.AddressData

Destination path:
IList`1[0].Address0[0].Address0[0]

Source value:
(null)
Source=AutoMapper
StackTrace:
   at App.Services.Managers.MembershipManager.GetAllMember() in C:\My Work\Credit Union Application\CreditSolutionApp\App.Services.Managers\Membership\MembershipManager.cs:line 35
   at TestingApp.Program.Main(String[] args) in C:\My Work\Credit Union Application\CreditSolutionApp\TestingApp\Program.cs:line 19
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

内部异常:

4

1 回答 1

0

我已经弄清楚了,因为忽略源中的所有空值

这是代码

 public IList<MemberData> GetAllMember()
    {
        IList<MemberData> MemberDataList = null;

        IList<Member> memberList = _MembershipCoreServices.GetAllMember();

         if (memberList != null)
         {
             var config = new MapperConfiguration(cfg =>
             {
                 cfg.CreateMap<Member, MemberData>()
                          .ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull));
             });

             IMapper mapper = config.CreateMapper();

             MemberDataList = mapper.Map<IList<MemberData>>(memberList).ToList();

         }


       return MemberDataList;

    }
于 2016-06-17T14:19:48.273 回答