【问题标题】:Automapper child entities not mappingAutomapper 子实体未映射
【发布时间】:2018-02-23 19:34:21
【问题描述】:

我正在尝试映射包含默认子实体的父实体。

所以,我有一个 promotion 对象,它有一个 localisedPromotion 对象的集合

我想设置一个特定的localisedPromotion 并将其设置为我的defaultLocalisedPromotion

在我的配置中,我有:

config.CreateMap<LocalisedPromotion, LocalisedPromotionViewModel>()
    .ForMember(dest => dest.LocalisedPromotionId, o => o.MapFrom(src => src.Id));

config.CreateMap<Promotion, PromotionViewModel>()
    .ForMember(dest => dest.DefaultLocalisedPromotion,
               o => o.MapFrom(src => src.LocalisedPromotions
                                        .FirstOrDefault(x => x.CultureId == src.DefaultCultureId)));

所以在我看来,这应该设置DefaultLocalisedPromotion 类型为LocalisedPromotionViewModel 的字段

但该字段始终为空

有没有明显的方法来做我想要实现的目标

【问题讨论】:

    标签: c# automapper


    【解决方案1】:

    我写了一个单元测试,似乎 AutoMapper 代码有效。请查看单元测试并让我知道它是否正确或发布更多代码(尤其是您要映射的类)以便调整测试。

    using AutoMapper;
    using Xunit;
    
    using System.Linq;
    using System.Collections.Generic;
    
    namespace AutoMapperTest
    {
        public class LocalisedPromotion
        {
            public int Id;
            public int CultureId;
        }
    
        public class LocalisedPromotionViewModel
        {
            public int LocalisedPromotionId;
        }
    
        public class Promotion
        {
            public List<LocalisedPromotion> LocalisedPromotions;
            public int DefaultCultureId;
        }
    
        public class PromotionViewModel
        {
            public LocalisedPromotionViewModel DefaultLocalisedPromotion;
        }
    
        public class AutoMapperTest
        {
            [Fact]
            public void Test()
            {
                var theConfig = new MapperConfiguration(config =>
                {
                    config.CreateMap<LocalisedPromotion, LocalisedPromotionViewModel>()
                        .ForMember(dest => dest.LocalisedPromotionId, o => o.MapFrom(src => src.Id));
    
                    config.CreateMap<Promotion, PromotionViewModel>()
                        .ForMember(dest => dest.DefaultLocalisedPromotion,
                                   o => o.MapFrom(src => src.LocalisedPromotions
                                                            .FirstOrDefault(x => x.CultureId == src.DefaultCultureId)));
                });
    
                var promotion = new Promotion { DefaultCultureId = 3 };
                promotion.LocalisedPromotions = new List<LocalisedPromotion>() { new LocalisedPromotion() { Id = 1, CultureId = 3 } };
    
                var result = theConfig.CreateMapper().Map<Promotion, PromotionViewModel>(promotion);
    
                Assert.NotNull(result.DefaultLocalisedPromotion);
                Assert.Equal(result.DefaultLocalisedPromotion.LocalisedPromotionId, 1);
            }
        }
    }
    

    【讨论】:

    • 这很奇怪,你的测试也适用于我,即使我将你的类替换为应用程序中使用的实际类。我猜需要进一步调查
    • 您是否已经找出导致代码问题的原因?
    • 是的,我做到了。它在测试中有效但在应用程序中无效的事实表明配置文件中有问题。我有 config.CreateMissingTypeMaps = true;在那个文件中。当我对此发表评论时,映射工作正常。感谢您的观看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多