C# 自动映射:在配置文件上设置AllowNullCollections

C# 自动映射:在配置文件上设置AllowNullCollections,c#,automapper,C#,Automapper,我正在使用Automapper将一个集合为null的类映射到一个集合相同的目标。我需要目标集合也为null 配置文件类上有一个名为AllowNullCollections的属性。它不会影响映射。 如果我将cfg.AllowNullCollections设置为True,则映射会将目标集合保留为null(如我所愿) 我无法为系统中的所有映射将AllowNullCollections设置为True,它只能应用于我的配置文件 using System.Collections.Generic; using

我正在使用Automapper将一个集合为null的类映射到一个集合相同的目标。我需要目标集合也为null

配置文件类上有一个名为AllowNullCollections的属性。它不会影响映射。 如果我将cfg.AllowNullCollections设置为True,则映射会将目标集合保留为null(如我所愿)

我无法为系统中的所有映射将AllowNullCollections设置为True,它只能应用于我的配置文件

using System.Collections.Generic;
using AutoMapper;
using NUnit.Framework;
using Assert = NUnit.Framework.Assert;


namespace Radfords.FreshCool.Web.Tests
{
    [TestFixture]
    [Category("UnitTest")]
    class AutomapperTests
    {
        private IMapper _mapper;

        // this says that AllowNullCollections does work at the profile level, in May. 
        //https://github.com/AutoMapper/AutoMapper/issues/1264

        [SetUp]
        public void SetUp()
        {

            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile<TestMappingProfile>();
                // I want the profile to set the configuration, if I set this here the test passes
                //cfg.AllowNullCollections = true;
            });
            _mapper = config.CreateMapper();
        }


        [Test]
        [Category("UnitTest")]
        public void MapCollectionsTest_MustBeNull()
        {
            var actual = _mapper.Map<Destination>(new Source());

            Assert.IsNull(actual.Ints, "Ints must be null.");
        }

    }

    internal class TestMappingProfile : Profile
    {
        public TestMappingProfile()
        {
            AllowNullCollections = true;

            CreateMap<Source, Destination>();
        }
    }

    internal class Source
    {
        public IEnumerable<int> Ints { get; set; }
    }

    internal class Destination
    {
        public IEnumerable<int> Ints { get; set; }
    }
}
使用System.Collections.Generic;
使用自动制版机;
使用NUnit.Framework;
使用Assert=NUnit.Framework.Assert;
命名空间Radfords.FreshCool.Web.Tests
{
[测试夹具]
[类别(“单元测试”)]
类自动映射测试
{
专用图像映射器;
//这表明AllowNullCollections在5月份的概要文件级别上确实有效。
//https://github.com/AutoMapper/AutoMapper/issues/1264
[设置]
公共作废设置()
{
var config=new-MapperConfiguration(cfg=>
{
AddProfile();
//我希望配置文件设置配置,如果我在这里设置,测试通过
//cfg.AllowNullCollections=true;
});
_mapper=config.CreateMapper();
}
[测试]
[类别(“单元测试”)]
公共void MapCollectionsTest_MustBeNull()
{
var-actual=_mapper.Map(新的Source());
Assert.IsNull(actual.Ints,“Ints必须为null”);
}
}
内部类TestMappingProfile:Profile
{
公共TestMappingProfile()
{
AllowNullCollections=true;
CreateMap();
}
}
内部类源
{
公共IEnumerable整数{get;set;}
}
内部类目的地
{
公共IEnumerable整数{get;set;}
}
}

您可以用以下内容替换TestMappingProfile Ctor,它应该可以工作:

public TestMappingProfile()
{

CreateMap<Source, Destination>().ForMember(dest => dest.Ints, opt => opt.Condition(src => (src.Ints != null)));            
}
公共TestMappingProfile()
{
CreateMap().formMember(dest=>dest.Ints,opt=>opt.Condition(src=>(src.Ints!=null));
}

威尔·雷在github上提交了一个问题。当前状态是不能在利润级别设置AllowNullCollections,必须在配置级别设置它。

我认为这是一个错误。我必须获得更多的信息,但有一点需要补充:“我不能为我的系统中的所有映射将AllowNullCollections设置为True,它必须只应用于我的配置文件。”-这就是我的意图。配置文件不更改全局设置,仅更改其映射类型的设置。感谢您的回复。是,为了澄清,我不能在配置中将allownullcollections设置为true,因为它将全局应用,我想在配置文件级别将allownullcollections设置为true,只是为了我的特定映射。是的,这段代码将在4.2x版中按照您的预期工作。5x中似乎发生了一些变化。感谢您的建议。我映射的类有很多集合,因此在成员级别指定映射并不理想。