首页 > 解决方案 > AutoMapper 10.0.0 with F# - 如何在没有额外映射的情况下修复它?

问题描述

我有 3 种类型:

type [<CLIMutable>] status = { id : Guid; name : string }
type [<CLIMutable>] container = { id : Guid; name : string; status : status}
type [<CLIMutable>] scontainer = { id : Guid; name : string; status : string}

和下一个配置

let c = 
        MapperConfiguration(
            fun config ->
                config.CreateMap<container, scontainer>()
                    .ForMemberFs((fun sc -> sc.name), (fun opts -> opts.MapFrom(fun m _ -> m.status.name))) |> ignore
        )

我正在尝试使用下一个代码进行映射

let con = { id = Guid.NewGuid(); name = "Template 1"; container.status = { id = Guid.NewGuid(); name = "Draft" } }
let mapper = c.CreateMapper()
let sc = mapper.Map<scontainer>(con)

但是没有调用成员映射,并且 sc.status 包含状态对象的字符串表示形式(id 和 name 一起)。

当我添加新地图时:

config.CreateMap<status, string>().ConvertUsing(fun s -> s.name) |> ignore

然后 sc.status 包含正确的值。

有谁知道如何在没有额外映射的情况下使其工作?

标签: automapper

解决方案


下一行解决了我的问题:

let c = 
        AutoMapper.MapperConfiguration(
            fun config ->
                config.ShouldMapProperty <- fun _ -> false
                config.ShouldMapField <- fun _ -> true
        )

推荐阅读