Enzo95
Enzo95

Reputation: 23

Automapper returns null

I've the abstract class AbsProduct

public abstract class AbsProduct
{
    [Key]
    public int ID { get; set; }
    public int Price { get; set; }
    public int Category { get; set; }
    public string Name { get; set; }

    public abstract double Accept(IProductVisitor visitor);

}

and the ProductDTO:

public class ProductDTO
{
    public int ID { get; set; }
    public int Price { get; set; }
    public int Category { get; set; }
    public string Name { get; set; }
}

My configuration is

AutoMapper.Mapper.Initialize(config =>
{
    config.CreateMap<AbsProduct, ProductDTO>();
    config.CreateMap<ProductDTO, AbsProduct>();
});

The problem is when I try to map ProductDTO to AbsProduct:

var product = AutoMapper.Mapper.Map<ProductDTO, AbsProduct>(productDTO);

AutoMapper is returning null, but the source(productDTO) isn't null.

Upvotes: 0

Views: 906

Answers (1)

Adrian Iftode
Adrian Iftode

Reputation: 15663

You can't instantiate an abstract class.

Try create a type that derives from AbsProduct and use that type instead of it.

Upvotes: 1

Related Questions