简体   繁体   中英

Automapper create destination type object with no values inside

i have come across a similar problem as mentioned in this question AutoMapper setting destination object to null

my mapper is creating an empty object of destination type although i can see values in my source object in debugger.

my source object is a grpc message of submitOrder type

syntax = "proto3";

option csharp_namespace = "SubmitOrderService";

package tracksubmitorder;

import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";

service SubmitOrderService {

  rpc SubmitOrder(submitOrder) returns (google.protobuf.Empty);
}

// The request message containing the user's name.
message submitOrder {
  int64 orderID= 1;
  int32 beef =2;
  int32 chicken =3;
  int32 tofu =4;
  int32 shrimp =5;
  google.protobuf.Timestamp createdOn = 20;
  string comment=30;
}

my destination is a data model

namespace SubmitOrderService.Models
{
    public class Submit_Order
    {
        public long Id { get; set; }
        public string Comments { get; set; }
        public DateTime CreatedAt { get; set; }
        public int CountBeef { get; set; }

        public int  CountChicken { get; set; }
        public int CountTofu { get; set; }
        public int CountShrimp { get; set; }        
    }
}

this is my profile file

using AutoMapper;
using SubmitOrderService.Models;

namespace SubmitOrderService
{
    public class SubmitOrderProfile: Profile
    {
        public SubmitOrderProfile()
        {
            CreateMap<submitOrder, Submit_Order>().ForMember(
                    dest => dest.Comments,
                    opt => opt.MapFrom(src => $"{src.Comment}")
                ).ForMember(
                    dest => dest.CreatedAt,
                    opt => opt.MapFrom(src => $"{src.CreatedOn }")
                )
                .ForMember(
                    dest => dest.CountChicken ,
                    opt => opt.MapFrom(src => $"{src.Chicken}")
                )
                .ForMember(
                    dest => dest.CountBeef ,
                    opt => opt.MapFrom(src => $"{src.Beef}")
                )
                .ForMember(
                    dest => dest.CountShrimp ,
                    opt => opt.MapFrom(src => $"{src.Shrimp}")
                )
                .ForMember(
                    dest => dest.CountTofu ,
                    opt => opt.MapFrom(src => $"{src.Tofu}")
                ).ReverseMap();

        }
    }
}

this is my line for mapping where reqOrder is of type submitOrder

var order1 = _mapper.Map<Submit_Order>(reqOrder);

ps. This is my first time trying out Automapper, so any kind of help will be appreciated.

solved the problem. I had to explicitly convert google.protobuf.Timestamp to DateTime for my CreatedAt member. like this,

ForMember( dest => dest.CreatedAt, opt => opt.MapFrom(src => src.CreatedOn.ToDateTime()) ) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM