AutoMapper map object to Lookup field in MS Dynamics CRM
I am trying to map using AutoMapper.
Source object is C# object
public class Source
{
public string comment { get; set; }
public string Subject { get; set; }
public Account Account{ get; set; }
}
public class Account
{
public string FirstName{ get; set; }
public string LastName{ get; set; }
}
My destination is a CRM entity named crm_destination which has fields like comment , Subject but it also has a LookUp field account of type Account .
But I don't know how to map LookUp field.
Following is my Automapper
AutoMapper.Mapper.CreateMap<Source, Destinaetion>()
.ForMember(dest => dest.comment, opt => opt.MapFrom(src => src.comment))
.ForMember(dest => dest.account, opt => opt.MapFrom(src => src.account));
.ForMember(dest => dest.account, opt => opt.MapFrom(src => src.account)) is throwing error of type mismatch..
basically my problem is I don't know how to map LookUp field where CRM entity is destination.
You need to use a custom resolver in AutoMapper. In your custom resolver you'll need to query CRM to get the ID of the account
record in that matches your Account
object and return an EntityReference to this object.
Lookups are stored as EntityReferences in CRM. So you'll need to convert your account to an EntityReference.
I have never used AutOMapper, but what Nicknow suggests makes sense. Just call the ToEntityReference()
method on Entity.
I solved it. Lookup creates a relationship. my lookp created a relationship name cxrm_account_cxrm_source_account
so instead of .ForMember(dest => dest.account, opt => opt.MapFrom(src => src.account))
I did
.ForMember(dest => dest.cxrm_account_cxrm_source_account, opt => opt.MapFrom(src => src.Account))
now it is working
链接地址: http://www.djcxy.com/p/37374.html上一篇: 将字符串映射到同名的对象中