如何使用automapper在json中映射类实体?

问题描述 投票:0回答:1

我将元信息存储到字符串字段中。但在DTO对象中,我使用分离的类对象进行映射。

示例JSON Post对象

{

  "name": "string",
  "directionId": 0,
  "description": "string",
  "siteId": 0,
  "zoneId": 0,
  "areaId": 0,
  "metaData": {
    "input": {
      "name": "string",
      "key": "string",
      "agentId": 0,
      "type": 0
    },
    "outPut": {
      "name": "string",
      "key": "string",
      "agentId": 0,
      "type": 0
    },
    "position": {
      "lat": "string",
      "long": "string"
    }
  }
}

实体类对象

/// <summary>
/// SRC  
/// </summary>
public class SRC
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? DirectionId { get; set; }
    public string Description { get; set; }
    public int? SiteId { get; set; }
    public int? ZoneId { get; set; }
    public int? AreaId { get; set; }

    /// <summary>
    /// Json view MetaData
    /// { 
    ///   "input": {
    ///      name:"my input1",
    ///       key:"43434",
    ///       agent:"1",
    ///       type:"1",
    ///    }
    ///    "output": {
    ///      name:"my output",
    ///       key:"12333",
    ///       agent:"1",
    ///       type:"1",
    ///    }
    ///    "position": {
    ///      lat:"42°21'30"N 71°03'37",
    ///       long:"42°21'30"S 71°03'37",
    ///    }
    /// }
    /// </summary>
    public string MetaData { get; set; }
}

DTO类对象

/// <summary>
/// SRC DTO
/// </summary>
public class SrcDTO
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? DirectionId { get; set; }
    public string Description { get; set; }
    public int? SiteId { get; set; }
    public int? ZoneId { get; set; }
    public int? AreaId { get; set; }
    [JsonProperty("MetaData")]
    public SourceMetaData MetaData { get; set; }
}
#region Meta Data Class

public class SRCMetaData
{
    [JsonProperty("Input")]
    SourceInput SourceInput { get; set; }
    [JsonProperty("OutPut")]
    SourceOutput SourceOutput { get; set; }
    [JsonProperty("Position")]
    SourcePosition SourcePosition { get; set; }
}
public class SourceInput
{
    public string Name { get; set; }
    public string Key { get; set; }
    public int AgentId { get; set; }
    public int Type { get; set; }
}
public class SourceOutput
{
    public string Name { get; set; }
    public string Key { get; set; }
    public int AgentId { get; set; }
    public int Type { get; set; }
}
public class SourcePosition
{
    public string Lat { get; set; }
    public string Long { get; set; }
}
#endregion

如何使用Auto Mapper配置文件进行映射?

   CreateMap<SrcDTO, Src>()
        .ForMember(x => x.MetaData, cfg => { cfg.MapFrom(jo => JsonConvert.SerializeObject(jo.MetaData)); })
        ;

        CreateMap<Src,  SrcDTO>()
        .ForMember(x=>x.MetaData , cfg => { cfg.MapFrom(jo => JsonConvert.DeserializeObject(jo.MetaData)); })
        ;

在发送JSON的帖子操作上,它正常工作。但是在GetALL操作中它不起作用,请参阅下面的图像以供参考。 enter image description here enter image description here

c# entity-framework automapper automapper-2
1个回答
1
投票

问题是在子类对象中映射“Int”数据类型字段。我已经修改了“字符串”数据类型字段。

随着JsonConvert.SerializeObject与铸造序列化,它对我有用..

谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.