tags:

views:

33

answers:

3

Hi,

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; set; }
  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?

A: 

AutoMapper only support the following collections out of the box: http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays&amp;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&amp;referringTitle=Home

Andreas Paulsson
A: 

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">

BeRecursive
A: 

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

Carsten Alder