So I have this WebApiPrice.Web.Api.Models.Category class
/*
* 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);
}
}
}
I am trying to map to this 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; }
}
}
My mapper configuration looks like this:
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());
}
}
}
My call to the mapper is here:
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 maps ok, but both Category and type does not. My HTTP request body looks like this:
{"Name":"Pelmenis", "Category": {"CategoryId":"1", "Name":"Food"}, "Type": {"TypeId":"1"}}
Blockquote
I tried to write category both with and without the name. newProduct.Category is filled with variables given and there are such values in the database.
What could be wrong? Is there some additional information I should provide?
BUMP I am really stuck, any suggestion would be valued here.