jirkos
jirkos

Reputation: 1

ASP.NET using AutoMapper

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

Upvotes: 0

Views: 86

Answers (1)

M Rizwan
M Rizwan

Reputation: 108

Try this

return Mapper.Map<List<Inventory>>(inventories);

Upvotes: 1

Related Questions