AutoMapper 根据另一个属性填充其他属性
AutoMapper populate other properties based on another one
我有这个场景
public class ObjectViewModel
{
public int Isn { get; set; }
public int? APO_Isn { get; set; }
public string Name { get; set; }
}
public class ObjectModel
{
public int Isn { get; set; }
public int? APO_Isn { get; set; }
public int? CLI_Isn { get; set; }
public int? EMP_Isn { get; set; }
}
//Configuration of AutoMapper: Mapper.CreateMap<ObjectViewModel, ObjectModel>().ReverseMap();
//On my controller: var objectModel= Mapper.Map<ObjectModel>(objectViewModel);
假设 objectViewModel 的 属性 等于 53。当我基于 objectViewModel 映射 objectModel 时,出于某种我不知道的原因,AutoMapper 在 Is 中输入值 53(没关系),但正在输入在 CLI_Isn 和 EMP_Isn 中也是如此,我的 ViewModel 没有这些属性。
我做了一些测试,将CLI_Isn更改为Cli_Isn,将EMP_Isn更改为Emp_Isn,问题就解决了。但我仍然不明白是什么导致了这个问题,这就是为什么 53 被移动到 CLI_Isn 和 EMP_Isn.
您的 Mapper.Initialize
是否可能包含对 RecognizeDestinationPrefixes("CLI_")
或 RecognizePrefixes("CLI_")
的调用?这将使 automapper
在映射类型时忽略 "CLI_" 但不会忽略 "Cli_".
你正在触发 AutoMapper 的 Flattening feature
来自该文档:
When you configure a source/destination type pair in AutoMapper, the configurator attempts to match properties and methods on the source type to properties on the destination type. If for any property on the destination type a property, method, or a method prefixed with "Get" does not exist on the source type, AutoMapper splits the destination member name into individual words (by PascalCase conventions).
因为 issue 402 was fixed 这似乎也适用于下划线,这就是您在这里看到的。
似乎无法禁用扁平化,因此您必须明确排除这些属性:
Mapper.CreateMap<ObjectViewModel, ObjectModel>()
.ForMember(dest => dest.CLI_Isn, src => src.Ignore())
.ForMember(dest => dest.EMP_Isn, src => src.Ignore())
.ReverseMap();
我有这个场景
public class ObjectViewModel
{
public int Isn { get; set; }
public int? APO_Isn { get; set; }
public string Name { get; set; }
}
public class ObjectModel
{
public int Isn { get; set; }
public int? APO_Isn { get; set; }
public int? CLI_Isn { get; set; }
public int? EMP_Isn { get; set; }
}
//Configuration of AutoMapper: Mapper.CreateMap<ObjectViewModel, ObjectModel>().ReverseMap();
//On my controller: var objectModel= Mapper.Map<ObjectModel>(objectViewModel);
假设 objectViewModel 的 属性 等于 53。当我基于 objectViewModel 映射 objectModel 时,出于某种我不知道的原因,AutoMapper 在 Is 中输入值 53(没关系),但正在输入在 CLI_Isn 和 EMP_Isn 中也是如此,我的 ViewModel 没有这些属性。
我做了一些测试,将CLI_Isn更改为Cli_Isn,将EMP_Isn更改为Emp_Isn,问题就解决了。但我仍然不明白是什么导致了这个问题,这就是为什么 53 被移动到 CLI_Isn 和 EMP_Isn.
您的 Mapper.Initialize
是否可能包含对 RecognizeDestinationPrefixes("CLI_")
或 RecognizePrefixes("CLI_")
的调用?这将使 automapper
在映射类型时忽略 "CLI_" 但不会忽略 "Cli_".
你正在触发 AutoMapper 的 Flattening feature
来自该文档:
When you configure a source/destination type pair in AutoMapper, the configurator attempts to match properties and methods on the source type to properties on the destination type. If for any property on the destination type a property, method, or a method prefixed with "Get" does not exist on the source type, AutoMapper splits the destination member name into individual words (by PascalCase conventions).
因为 issue 402 was fixed 这似乎也适用于下划线,这就是您在这里看到的。
似乎无法禁用扁平化,因此您必须明确排除这些属性:
Mapper.CreateMap<ObjectViewModel, ObjectModel>()
.ForMember(dest => dest.CLI_Isn, src => src.Ignore())
.ForMember(dest => dest.EMP_Isn, src => src.Ignore())
.ReverseMap();