Automapper returns 空对象,源对象不为空。为什么?
Automapper returns null object, source object is not null. Why is that so?
所以我有这个WebApiPrice.Web.Api.Models.Categoryclass
/*
* 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 class:
/*
* 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>();
}
所以我有这个WebApiPrice.Web.Api.Models.Categoryclass
/*
* 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 class:
/*
* 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>();
}