Question

I'm setting up a mapping between some nice, light weight POCOs and some big fat heavy data aware objects that are part of a legacy architecture.

The data objects are using SqlTypes for their properties - so I've got Data.Role.Name as SqlString but Poco.Role.Name as string.

Automapper is set up as such:

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

On the attempt to map Mapper.Map(pocoRole, dataRole), the exception (scroll down) is thrown.

How can I cause Automapper to map from string to sqlstring gracefully?

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) 
Était-ce utile?

La solution

I think you could use type converters for that. Look at this article:

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

Autres conseils

For that member, you should have to do something like:

.ForMember(d => d.Name, opt => opt.MapFrom(s => new SqlString(s.Name));

Or you could use the ContructUsing method.

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

Taking this approach will convert all String, SqlString pairs using the constructor so you don't need specific .ForMember maps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top