C# 自动映射返回空对象,源对象不为空。为什么会这样?
所以我有这个WebApiPrice.Web.Api.Models.Category类C# 自动映射返回空对象,源对象不为空。为什么会这样?,c#,asp.net,.net,automapper,asp.net-web-api2,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 Lis
/*
* 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);
}
}
}
/*
*服务模型类,它将表示类别
*/
使用制度;
使用System.Collections.Generic;
命名空间WebApiPrice.Web.Api.Models
{
公共类类别
{
私人列表链接;
公共长类别ID{get;set;}
公共字符串名称{get;set;}
公共日期时间时间戳{get;set;}
公共列表产品{get;set;}
公共列表链接
{
获取{return\u links???(\u links=new List());}
设置{u links=value;}
}
公共无效添加链接(链接链接)
{
链接。添加(链接);
}
}
}
我正在尝试映射到此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; }
}
}
/*
*数据库表类别的域类
*/
使用制度;
使用System.Collections.Generic;
命名空间WebApiPrice.Data.Entities
{
公共类类别:无公害性
{
私有只读IList_products=新列表();
公共虚拟长类别ID{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟用户通过{get;set;}创建
公共虚拟用户由{get;set;}编辑
公共虚拟日期时间时间戳{get;set;}
公共虚拟IList产品
{
获取{return\u products;}
}
公共虚拟字节[]版本{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());
}
}
}
使用AutoMapper;
使用WebApiPrice.Common.TypeMapping;
使用WebApiPrice.Web.Api.Models;
命名空间WebApiPrice.Web.Api.AutoMappingConfiguration
{
公共类CategoryCategoryEntityAutoMapperTypeConfigurator:IAutoMapperTypeConfigurator
{
public void Configure()
{
Mapper.CreateMap()
.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;
}
公共产品添加产品(新产品新产品)
{
var productEntity=_autoMapper.Map(新产品);
productEntity.Category=\u autoMapper.Map(newProduct.Category);
productEntity.Type=\u autoMapper.Map(newProduct.Type);
_queryProcessor.AddProduct(productEntity);
var product=_autoMapper.Map(productEntity);
退货产品;
}
newProduct到Product映射正常,但类别和类型都不正常。我的HTTP请求正文如下所示:
{“名称”:“Pelmenis”,“类别”:{“CategoryId”:“1”,“名称”:“Food”},
“类型”:{“类型ID”:“1”}
大宗报价
我试着写有名字和没有名字的分类。类别由给定的变量填充,数据库中有这样的值
有什么不对劲吗?我是否应该提供一些额外的信息
BUMP我真的被卡住了,任何建议都将在这里得到重视。尝试删除映射器中的忽略线:
public void Configure() {
Mapper.CreateMap<Category, Data.Entities.Category>();
}
public void Configure(){
CreateMap();
}
尝试映射类别时得到的输出是什么?为什么你在问题中展示的映射器会忽略除产品列表之外的所有属性?映射后,我得到这样一个对象:谢谢,它成功了。唯一的问题是它没有根据类别和类型各自的ID恢复它们的其他属性。AutoMapper是否应该恢复它们?AutoMapper仅将各个字段从一个对象映射到另一个对象。任何附加信息都应手动分配。例如,可以使用MapFrom
或ResolveUsing
方法设置这些值。看见