Преобразование типа значения Automapper в SqlType - PullRequest
0 голосов
/ 02 февраля 2012

Я настраиваю отображение между некоторыми хорошими, легкими POCO и некоторыми большими объектами с большими объемами данных, которые являются частью устаревшей архитектуры.

Объекты данных используют SqlTypes для своих свойств - поэтому я получил Data.Role.Name в качестве SqlString, но Poco.Role.Name в качестве строки.

Automapper настроен так:

Mapper.CreateMap<Role, Data.Role>()
    .ForMember(dest => dest.Role_ID, 
                opt => opt.MapFrom(src=>src.ID));

При попытке сопоставления Mapper.Map(pocoRole, dataRole) выдается исключение (прокрутка вниз).

Как сделать так, чтобы Automapper изящно отображал из строки в sqlstring?

AutoMapper.AutoMapperMappingException : Trying to map Poco.Role to Data.Role.
Using mapping configuration for Poco.Role to Data.Role
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
  ----> AutoMapper.AutoMapperMappingException : Trying to map System.String to System.Data.SqlTypes.SqlString.
Using mapping configuration for Poco.Role to Data.Role
Destination property: Name
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
  ----> AutoMapper.AutoMapperMappingException : Trying to map System.String to System.Data.SqlTypes.SqlString.
Using mapping configuration for Poco.Role to Data.Role
Destination property: Name
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
  ----> AutoMapper.AutoMapperMappingException : Trying to map System.String to System.Data.SqlTypes.SqlString.
Using mapping configuration for Poco.Role to Data.Role
Destination property: Name
Missing type map configuration or unsupported mapping.
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.

at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
at AutoMapper.MappingEngine.Map(Object source, Object destination, Type sourceType, Type destinationType)
at AutoMapper.MappingEngine.Map(TSource source, TDestination destination)
at AutoMapper.Mapper.Map(TSource source, TDestination destination)
at Repository.RoleRepository.FindAll(IRole filter) in RoleRepository.cs: line 31
at Repository.ReaderRepositoryBase`1.Find(TEntity filter) in ReaderRepositoryBase.cs: line 30
at Test.RepositoryTests.GetRoleInternal() in RepositoryTests.cs: line 79
--AutoMapperMappingException
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
--AutoMapperMappingException
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap)
--AutoMapperMappingException
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) 

Ответы [ 3 ]

2 голосов
/ 02 февраля 2012

Я думаю, вы могли бы использовать для этого преобразователи типов. Посмотрите на эту статью:

http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters

2 голосов
/ 02 февраля 2012

Для этого участника вы должны сделать что-то вроде:

.ForMember(d => d.Name, opt => opt.MapFrom(s => new SqlString(s.Name));
1 голос
/ 02 февраля 2012

Или вы можете использовать метод ContructUsing.

Mapper.CreateMap<String, SqlString>().ConstructUsing(s => new SqlString(s));

При таком подходе будут преобразованы все пары String, SqlString с использованием конструктора, поэтому вам не нужны конкретные .ForMember карты.

...