AutoMapper忽略属性

时间:2010-10-12 06:02:04

标签: c# automapper

当我使用此映射时

           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);

这个班级

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

应忽略某些属性(例如,区域)。字符串属性似乎始终正确映射。我做错了什么?

3 个答案:

答案 0 :(得分:0)

AutoMapper仅支持开箱即用的以下集合:http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays&referringTitle=Home。我猜你没有复制的属性是XXXCollection类型。

您可以通过为您的收藏类型创建自定义类型转换器来解决此问题:http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters&referringTitle=Home

答案 1 :(得分:0)

对于您的收藏品,您需要执行与以下类似的操作(取自我最近处理过的一些代码):

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

QuizDataCompositeModelQuiz.DataContracts.QuizDataComposite都延伸List<"RespectiveType">

答案 2 :(得分:0)

这很简单:

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

Tipp:下载源代码并从给定的单元测试和样本中学习! 你可以在那里得到它:http://automapper.codeplex.com/SourceControl/list/changesets