在数据对象之间进行映射时出现 C# AutoMapper 错误
C# AutoMapper error while mapping between data object
我有业务实体对象,我需要使用 AutoMapper 映射到 WCF 服务的数据合同对象,但我在 'MemberDataList' 处收到错误我有相同版本的数据实体 class 'Member' 来自我用来映射到业务对象的数据访问层 class 并且它工作正常,不知道为什么这个有问题
来源Class
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; }
}
目的地Class
[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; }
}
}
映射Class
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()
内部异常:
我已经弄明白了,忽略源中的所有空值
这是代码
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;
}
我有业务实体对象,我需要使用 AutoMapper 映射到 WCF 服务的数据合同对象,但我在 'MemberDataList' 处收到错误我有相同版本的数据实体 class 'Member' 来自我用来映射到业务对象的数据访问层 class 并且它工作正常,不知道为什么这个有问题
来源Class
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; }
}
目的地Class
[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; }
}
}
映射Class
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()
内部异常:
我已经弄明白了,忽略源中的所有空值
这是代码
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;
}