C# Automapper未从包含单个可空布尔字段的源映射
我在Automapper中看到了一些非常奇怪的行为,当从一个具有单个C# Automapper未从包含单个可空布尔字段的源映射,c#,automapper,C#,Automapper,我在Automapper中看到了一些非常奇怪的行为,当从一个具有单个null属性的对象进行映射时。我目前的设置如下: public class MyViewModel { public bool? IsAThing { get; set; } } public class MyEntity { public bool? IsAThing { get; set; } public bool? HasAnotherThing { get; set; } publ
null
属性的对象进行映射时。我目前的设置如下:
public class MyViewModel
{
public bool? IsAThing { get; set; }
}
public class MyEntity
{
public bool? IsAThing { get; set; }
public bool? HasAnotherThing { get; set; }
public string AnotherThing { get; set; }
// Lots of other fields
}
使用以下映射配置文件(使用类似的反向映射):
dest.IsAThing
为空。映射配置文件是将MyViewModel
声明为映射源的唯一位置。奇怪的是,如果我声明类
public class AnotherThingViewModel
{
public bool? HasAnotherThing { get; set; }
public string AnotherThing { get; set; }
}
并进行以下测试:
var source = new AnotherThingViewModel { HasAnotherThing = true };
var dest = new MyEntity();
mapper.Map(source, dest);
dest.hasaotherthing
如预期的那样是true
显然,我不知道这里发生了什么,所以以前有没有人见过类似的情况,或者知道AutoMaper中可能导致这种情况的任何错误?我发现了,我在映射配置的其他地方有
CreateMap().ForAllMembers(opt=>opt.Ignore())
无法使用提供的代码和automapper nuget软件包的最新(6.1.1)版本(即-dest.IsAThing
为true且不为null)复制此内容。请编辑您的问题以包含,以便我们可以查看/测试您的源代码。
public class AnotherThingViewModel
{
public bool? HasAnotherThing { get; set; }
public string AnotherThing { get; set; }
}
var source = new AnotherThingViewModel { HasAnotherThing = true };
var dest = new MyEntity();
mapper.Map(source, dest);