c# - Automapper 返回空对象,源对象不为空。为什么会这样?

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

{"Name":"Pelmenis", "Category": {"CategoryId":"1", "Name":"Food"}, "Type": {"TypeId":"1"}}

Blockquote

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

有什么问题吗?我应该提供一些额外的信息吗?

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

最佳答案

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

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

关于c# - Automapper 返回空对象,源对象不为空。为什么会这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30395681/

相关文章:

c# - SignalR C# MVC 将匿名用户映射到客户端 ID

c# - XAML MVVM DatePicker - 如何获取数据?

.net - SqlConnection的Dispose方法会干扰连接池吗?

asp.net - 将多个模型从一个 View 传递到 Controller

c# - 从多个一维高效地创建矩形二维阵列

c# - 增量运算符如何与数组一起使用?

c# - 线程安全缓存机制(不是.NET内置缓存) ASPX C#

c# - 如何在 Xamarin.iOS 中使用 danielgindi 的 iOS 图表库?

asp.net - XSLT 母版页?

javascript - 如何在离线模式下加载 impress.js 的 strut 幻灯片编辑器