Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper with Dependency Injection Not Mapping Profiles?

so this is my first Stack Overflow question and I hope do I good job of giving enough detail for help. I'm trying to use AutoMappper with dotnet 3.1 with dependency injection however it doesn't seem to be registering my maps the way it says it does.

The error I am receiving:

AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
User -> UserCustomerDto

In my Startup.cs I have : services.AddAutoMapper(typeof(Startup)); Inside the IServiceCollection area

I have my User model and my various DTO models created.

Here is an example Profile model that I have:

    public class UserCustomerProfile : Profile
    {
        public UserCustomerProfile()
        {
            CreateMap<User, UserCustomerDto>();
            CreateMap<UserCustomerDto, User>();
        }
    }

My DTO Models have all the fields that my User Model has minus a few, the ones I do not want to surface to the UI that is.

The documentation has been rather difficult to follow as there is examples to non-dependency injection as well as pre-9.0 where configuration was required during the startup of the project.

I also tried using .ReverseMap() on the CreateMap() but that didn't work either.

I am injecting inside the service I am using:

        private readonly IMapper _mapper;

        public UserService(IMapper mapper)
        {
            _mapper = mapper;
        }

And where the error is spit out is inside that service with this code: _mapper.Map<UserCustomerDto>(user)

I'm fairly new to dotnet core , and C# in general so any help would be greatly appreciated! Thank you and I hope this was informative enough to point me in the right direction.

UPDATE: When moving the Profile models to the same project as my Startup everything works fine. Now I just need to know how I can add reference to the other project containing my Profile models. Or maybe I can't? Would be nice to keep it in my Models project where the rest of my models are.

like image 726
Collin Herber Avatar asked Oct 20 '25 03:10

Collin Herber


1 Answers

I think you haven't registered your automapper Profiles into the services container.

The setup for dotnet core is here

https://docs.automapper.org/en/stable/Dependency-injection.html#asp-net-core

Add the extra nuget package for dotnet core called AutoMapper.Extensions.Microsoft.DependencyInjection

If your profiles are in the same project you can add this code in startup

services.AddAutoMapper(Assembly.GetExecutingAssembly());

If your automapper profiles are in another project

// where UserCustomerProfile is any class in the other project
services.AddAutoMapper(
    Assembly.GetAssembly(typeof(UserCustomerProfile)));

It will search for all profiles and register them into the services list.

like image 154
Rosco Avatar answered Oct 21 '25 17:10

Rosco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!