2

I am trying to map two objects that are mostly similar with AutoMapper but one member (AudioSummary) raises the following exception :

The following property on EchoNestModel.AudioSummary cannot be mapped: AudioSummary

Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type EchoNestModel.AudioSummary.

Context:

- Mapping to property AudioSummary from EchoNest.Api.AudioSummary to EchoNestModel.AudioSummary

- Mapping from type EchoNest.Api.TrackProfile to EchoNestModel.Profile

Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.

Mapping definition

var map = Mapper.CreateMap<TrackProfile, Profile>();
map.ForMember(dest => dest.ForeignIds, opt => opt.ResolveUsing<ForeignIdResolver>());
map.ForMember(dest => dest.ForeignReleaseIds, opt => opt.ResolveUsing<ForeignReleaseIdResolver>());
map.ForMember(s => s.Media, t => t.Ignore());
map.ForMember(s => s.ProfileId, t => t.Ignore());
map.ForMember(s => s.AudioSummary, t => t.MapFrom(s => s.AudioSummary));

I've added the following two lines but a totally different error occurs :

map.ForMember(s => s.AudioSummary.Profile, t => t.Ignore());
map.ForMember(s => s.AudioSummary.AudioSummaryId, t => t.Ignore());

Expression 's => s.AudioSummary.Profile' must resolve to top-level member and not any child object's properties.

Use a custom resolver on the child type or the AfterMap option instead.

Parameter name: lambdaExpression

How can I successfully map AudioSummary ?

Source object

enter image description here

Target object enter image description here

4

1 回答 1

2

EDIT: In general, try AutoMapper.Mapper.AssertConfigurationIsValid();, this will show you all possible problems in your mapper setup.

From the information you provided, it looks like you need to define map for the AudioSummary classes (dest and source) as well:

    [TestFixture]
    public class MappingTest
    {
        public class SourceAudioSummary
        {
            public int Id { get; set; }
            public string OtherData { get; set; }
        }

        public class TrackProfile
        {
            public string Whatever { get; set; }
            public SourceAudioSummary AudioSummary { get; set; }
        }

        public class DestAudioSummary
        {
            public int Id { get; set; }
            public string OtherData { get; set; }
        }

        public class Profile
        {
            public string Whatever { get; set; }
            public DestAudioSummary AudioSummary { get; set; }
        }

        [Test]
        public void Mapping()
        {
            Mapper.CreateMap<SourceAudioSummary, DestAudioSummary>();
            Mapper.CreateMap<TrackProfile, Profile>();

            var trackProfile = new TrackProfile
                {
                    Whatever = "something",
                    AudioSummary = new SourceAudioSummary
                        {
                            Id = 1,
                            OtherData = "other"
                        }
                };

            var profile = Mapper.Map<TrackProfile, Profile>(trackProfile);

            Assert.That(profile.Whatever == "something");
            Assert.That(profile.AudioSummary.Id == 1);
            Assert.That(profile.AudioSummary.OtherData == "other");
        }
    }
于 2013-05-15T15:29:44.237 回答