自动映射值类型为SqlType转换

时间:2012-02-02 15:39:33

标签: c# automapper sql-types

我正在设置一些漂亮,重量轻的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 个答案:

答案 0 :(得分:2)

对于该成员,您应该执行以下操作:

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

答案 1 :(得分:2)

我认为您可以使用类型转换器。看看这篇文章:

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

答案 2 :(得分:1)

或者您可以使用ContructUsing方法。

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

采用这种方法将使用构造函数转换所有String,SqlString对,因此您不需要特定的.ForMember映射。