-1

I use AutoMapper in my .NET CORE 2.2 project.

I get this exception:

Missing type map configuration or unsupported mapping. Mapping types: SaveFridgeTypeModel -> FridgeType College.Refrigirator.Application.SaveFridgeTypeModel -> College.Refrigirator.Domain.FridgeType

On This row:

var fridgeType = _mapper.Map<SaveFridgeTypeModel, FridgeType>(model);

Here is defenition of FridgeType class:

public class FridgeType : IEntity , IType
{
    public FridgeType()
    {
        Fridges = new HashSet<Fridge>();
    }
    public int ID { get; set; }
    //Description input should be restricted 
    public string Description { get; set; }
    public string Text { get; set; }
    public ICollection<Fridge> Fridges { get; private set; }
}

Here is defenition of SaveFridgeTypeModel class:

public class SaveFridgeTypeModel
{
    public string Description { get; set; }
    public string Text { get; set; }
}

I add this row:

    services.AddAutoMapper(typeof(Startup));

To ConfigureServices function in Startup class.

UPDATE

I forgot to add mappin configuration to the post.

Here is mapping configs class:

public class ViewModelToEntityProfile : Profile
{
    public ViewModelToEntityProfile()
    {
        CreateMap<SaveFridgeTypeModel, FridgeType>();
    }
}

Any idea why I get the exception above?

Michael
  • 13,950
  • 57
  • 145
  • 288

2 Answers2

2

You need to use the type from the assembly where your maps are when registering automapper with DI.

AddAutomapper(typeof(ViewModelToEntityProfile));

If you had multiple assemblies with maps - you could use another overload:

AddAutomapper(typeof(ViewModelToEntityProfile), typeof(SomeOtherTypeInOtherAssembly));
Vidmantas Blazevicius
  • 4,652
  • 2
  • 11
  • 30
-1

After creating mapping config class you need to add the AutoMapperConfiguration in the Startup.cs as shown below:

  public void ConfigureServices(IServiceCollection services) {
    // .... Ignore code before this

   // Auto Mapper Configurations
    var mappingConfig = new MapperConfiguration(mc =>
    {
        mc.AddProfile(new ViewModelToEntityProfile());
    });

    IMapper mapper = mappingConfig.CreateMapper();
    services.AddSingleton(mapper);

    services.AddMvc();
}
Voodoo
  • 1,550
  • 1
  • 9
  • 19