Automapper返回null对象,源对象不为null。为什么会这样?

时间:2015-05-22 11:35:44

标签: c# asp.net .net automapper asp.net-web-api2

所以我有这个WebApiPrice.Web.Api.Models.Category类

     /*
 * Service model class, that will represent categories
 */

using System;
using System.Collections.Generic;

namespace WebApiPrice.Web.Api.Models
{
    public class Category
    {
        private List<Link> _links;

        public long CategoryId { get; set; }
        public string Name { get; set; }
        public DateTime Timestamp { get; set; }
        public List<Product> Products { get; set; }

        public List<Link> Links
        {
            get { return _links ?? (_links = new List<Link>()); }
            set { _links = value; }
        }
        public void AddLink(Link link)
        {
            Links.Add(link);
        }
    }
}

我正在尝试映射到此WebApiPrice.Data.Entities.Category类:

/*
 *  Domain class for database table Category
 */
using System;
using System.Collections.Generic;

namespace WebApiPrice.Data.Entities
{
    public class Category : IVersionedEntity
    {
        private readonly IList<Product> _products = new List<Product>();
        public virtual long CategoryId { get; set; }
        public virtual string Name { get; set; }
        public virtual User CreatedBy { get; set; }
        public virtual User EditedBy { get; set; }
        public virtual DateTime Timestamp { get; set; }
        public virtual IList<Product> Products
        {
            get { return _products; }
        }
        public virtual byte[] Version { get; set; }
    }
}

我的映射器配置如下所示:

using AutoMapper;
using WebApiPrice.Common.TypeMapping;
using WebApiPrice.Web.Api.Models;

namespace WebApiPrice.Web.Api.AutoMappingConfiguration
{
    public class CategoryToCategoryEntityAutoMapperTypeConfigurator : IAutoMapperTypeConfigurator
    {
        public void Configure()
        {
            Mapper.CreateMap<Category, Data.Entities.Category>()
                .ForMember(opt => opt.Version, x => x.Ignore())
                .ForMember(opt => opt.CreatedBy, x => x.Ignore())
                .ForMember(opt => opt.CategoryId, x => x.Ignore())
                .ForMember(opt => opt.Name, x => x.Ignore())
                .ForMember(opt => opt.EditedBy, x => x.Ignore())
                .ForMember(opt => opt.Timestamp, x => x.Ignore());
        }
    }
}

我对映射器的调用是:

public Product AddProduct(NewProduct newProduct)
        {
            var productEntity = _autoMapper.Map<Data.Entities.Product>(newProduct);
            productEntity.Category = _autoMapper.Map<Data.Entities.Category>(newProduct.Category);
            productEntity.Type = _autoMapper.Map<Data.Entities.Type>(newProduct.Type);

            _queryProcessor.AddProduct(productEntity);

            var product = _autoMapper.Map<Product>(productEntity);

            return product;
        }

newProduct to Product map ok,但Category和type都没有。我的HTTP请求正文如下所示:

  

{“姓名”:“Pelmenis”,“类别”:{“CategoryId”:“1”,“名称”:“食物”},   “类型”:{“TypeId”:“1”}}

     

块引用

我尝试使用和不使用名称来编写类别。 newProduct.Category填充了给定的变量,数据库中有这样的值。

可能有什么不对?我应该提供一些额外的信息吗?

BUMP我真的被困了,任何建议都会在这里受到重视。

1 个答案:

答案 0 :(得分:1)

尝试删除映射器中的忽略行:

public void Configure() {
    Mapper.CreateMap<Category, Data.Entities.Category>();
}