У меня есть два источника, таких как
public class Source1
{
public int Id {get;set;}
public int Source2ID { get; set; }
... //Other Fields
}
Source2, таких как
public class Source2 : Entity
{
public int Id {get;set;}
public string Name { get; set; }
}
И класс назначения следующим образом
public class destination
{
public int Id {get;set;}
public int Source2ID { get; set; }
public string Source2Name {get;set;}
... //Other Fields
}
Чего я пытаюсь достичьсопоставить имя source2 в месте назначения с source2ID, доступным в source1.
Я попытался использовать собственный преобразователь значений.
public class CustomResolver : IValueResolver<Source1, Destination, string>
{
private readonly IRepository<Source2> _suiteRepository;
public CustomResolver(IRepository<Source2> suiteRepository )
{
_suiteRepository = suiteRepository;
}
public string Resolve(Source1 source, Destination destination, string destMember, ResolutionContext context)
{
return _suiteRepository.Get(source.Source2ID).Name;
}
}
А затем в конфигурации яСоздаю карту следующим образом.
config.CreateMap<Source1, Destination>()
.ForMember(u => u.Name, options => options.ResolveUsing<CustomResolver>());
Ниже приведен код, который вызывает маппер
var source1 = await _repository
.GetAll()
.ToListAsync();
var destinationList = ObjectMapper.Map<List<Destination>>(source1);
Это, однако, приводит к следующей ошибке.
An unhandled exception occurred while processing the request.
MissingMethodException: No parameterless constructor defined for this object.
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, bool publicOnly, bool wrapExceptions, ref bool canBeCached, ref RuntimeMethodHandleInternal ctor)
AutoMapperMappingException: Error mapping types.
Mapping types:
Destination
Source1
Type Map configuration:
Source1-> Destinatinon
Namespace.Source1 -> Namespace.Destination
Property:
Source2Name
lambda_method(Closure , List<Sourc1> , List<Destination> , ResolutionContext )
AutoMapperMappingException: Error mapping types.
Я новичок в AutoMapper, я искал Google, но не мог найти что-то по этому вопросу.Я не уверен, есть ли лучший способ сопоставить их вместе.
Спасибо