【问题标题】:Automapper returns null object, source object is not null. Why is that so?Automapper 返回空对象,源对象不为空。为什么呢?
【发布时间】:2015-05-22 11:35:44
【问题描述】:

所以我有这个 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 到 Product 映射正常,但 Category 和 type 都没有。我的 HTTP 请求正文如下所示:

{"Name":"Pelmenis", "Category": {"CategoryId":"1", "Name":"Food"}, “类型”:{“TypeId”:“1”}}

块引用

我尝试写有名称和没有名称的类别。 newProduct.Category 填充了给定的变量,并且数据库中有这样的值。

可能出了什么问题?我应该提供一些其他信息吗?

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

【问题讨论】:

  • 尝试映射类别时得到的输出是什么?为什么您在问题中显示的映射器会忽略除产品列表之外的所有属性?
  • 映射后我得到这样的对象:

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


【解决方案1】:

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

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

【讨论】:

  • 谢谢,它成功了。唯一的问题是它没有根据各自的 ID 恢复 Category 和 Type 的其他属性。 AutoMapper 是否应该恢复它们?
  • AutoMapper 仅将各个字段从一个对象映射到另一个对象。任何附加信息都应手动分配。例如,您可以使用MapFromResolveUsing 方法设置这些值。见this
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
  • 2020-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多