0

I am learning ASP.NET by examples from the book on C# and that book was written at time when AutoMapper supported the method Initialize. I tried to circumvent it but not successfully. I replaced :

//    Mapper.Initialize(
  //    cfg =>
  //    {
  //      cfg.CreateMap<Inventory, Inventory>()
  //.ForMember(x => x.Orders, opt => opt.Ignore());
  //    });

with:

var config = new MapperConfiguration(cfg => cfg.CreateMap<Inventory, Inventory>()
       .ForMember(x => x.Orders, opt => opt.Ignore()));

and it seems (at least do not show errors in VS 2019).

But in the following:

// GET: api/Inventory
[HttpGet, Route("")]
public IEnumerable<Inventory> GetInventory()
{
    var inventories = _repo.GetAll();
    return Mapper.Map<List<Inventory>, List<Inventory>>(inventories);
}

I get an error:

An object reference is required for the non-static field, method, or property 'Mapper.Map<Inventory, Inventory>(Inventory)

Can somebody help me? Thanks. I am a newcomer here. Jiri

2
  • To be able to determine where the mapping is going wrong it would be useful to see the Inventory type. Commented Apr 11, 2022 at 8:00
  • namespace AutoLotDAL.Models { [Table("Inventory")] public partial class Inventory:EntityBase { [StringLength(50)] public string Make { get; set; } [StringLength(50)] public string Color { get; set; } [StringLength(50)] public string PetName { get; set; } public virtual ICollection<Order> Orders { get; set; } = new HashSet<Order>(); Commented Apr 11, 2022 at 17:58

1 Answer 1

1

Try this

return Mapper.Map<List<Inventory>>(inventories);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you anyway, but it does the same error.
I have use Automapper same way but I built the separate displayModel like this. var config = new MapperConfiguration(cfg => { cfg.CreateMap<ProductModel, ProductDisplayModel>(); });
var productList = _productEndPoint.GetAll(); var products=Mapper.Map<List<ProductDisplayModel>>(productList);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.