开发者

AutoMapper ignores Properties

开发者 https://www.devze.com 2023-01-20 02:23 出处:网络
when I\'m using this mapping Mapper.CreateMap<DataSourceConfigurationContract, DataSourceConfigurationContract>().ForMember(x => (object)x.DatabaseTypeException, opt => opt.Ignore())

when I'm using this mapping

           Mapper.CreateMap<DataSourceConfigurationContract, DataSourceConfigurationContract>().ForMember(x => (object)x.DatabaseTypeException, opt => opt.Ignore())
                                                                                               .ForMember(x => (object)x.DatabaseType, opt => opt.Ignore());

           var mappedValue = Mapper.Map<DataSourceConfigurationContract, DataSourceConfigurationContract>(dataSourceConfiguration);

for this class

public sealed class DataSourceConfigurationContract {
  public string Name { get; set; }
  public string ConnectionString { get; set; }
  public string ConnectionType  { get; set; }
  public DataSourcePropertyContractCollection Properties { get; set; }
  public DataSourceAreaConfigurationContractCollection Areas { get; set; }
  public UserContractCollection AllowedUsers{ get; set; }
  public DataSourceType? DatabaseType { get; s开发者_如何学运维et; }
  public ExceptionContract DatabaseTypeException { get; set; }
  public DataSourceType DataSourceType { get; set; } }

some Properties are ignored (e.g. Areas) that should be mapped. The string properties seem to be always correctly mapped. What have I done wrong?


AutoMapper only support the following collections out of the box: http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays&referringTitle=Home . I guess that your properties that are not copied are of type XXXCollection.

You can solve this by creating a custom type converter for your collection types: http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters&referringTitle=Home


For your collections you need to do something similar to the following (taken from some code I've recently worked on):

Mapper.CreateMap<List<QuizItemTypeModel>, List<Quiz.DataContracts.QuizItemType>>()
                .Include<QuizDataCompositeModel, Quiz.DataContracts.QuizDataComposite>();

Where QuizDataCompositeModel and Quiz.DataContracts.QuizDataComposite both extend List<"RespectiveType">


It's quite simple:

Mapper.CreateMap<DataSourceAreaConfigurationContract, DataSourceAreaConfiguration>();
Mapper.CreateMap<DataSourceConfigurationContract, DataSourceConfigurationContract>()
      .ForMember(dest => dest.Areas, opt => opt.UseDestinationValue());

Tipp: Download the source code and learn from the given unittests and samples! You can get it there: http://automapper.codeplex.com/SourceControl/list/changesets

0

精彩评论

暂无评论...
验证码 换一张
取 消