Autopilot error

Automapper throws this error: Missing map from String to String. Create using Mapper.CreateMap<String, String>.

The card is used in two places. in one place it works great, in another it fails.

The display profile is as follows:

public class AdminUserProfileProfile: Profile
{
    protected override void Configure()
    {
        Mapper.CreateMap<AdminUser, AdminUserProfile>()
              .ForMember(vm => vm.Id, opt => opt.MapFrom(m => m.Id))
              .ForMember(vm => vm.Name, opt => opt.MapFrom(m => m.Name))
              .ForMember(vm => vm.Email, opt => opt.MapFrom(m => m.Email))
              .ForMember(vm => vm.Roles, opt => opt.MapFrom(m => m.Roles.Select(r => r.Name)))
              .IgnoreAllNonExisting();
    }
}

The only difference in the case of use is that a map that behaves as expected uses Mapper.Map<AdminUserProfile>(entity), and one that fails is used through `Project (). To 'call.

I would like to use the projection features Project().To<>, what do I need to do to get this to work?

+4
source share
1 answer

AutoMapper, . - , automapper .

TL; DR: AdminUserProfile.Roles IEnumerable<string>, , .

.ForMember(vm => vm.Roles, opt => opt.MapFrom(m => m.Roles.Select(r => r.Name)))

, AdminUserProfile.Roles - - string[], ICollection<string> List<string>.

AutoMapper linq, :

admins.Select(a => new AdminUserProfile{
  Id = a.Id,
  Name = a.Name,
  Roles = a.Roles.Select(r => r.Name)
})

, Roles = a.Roles.Select(r => r.Name) . Roles ICollection<string>, a.Roles.Select IEnumerable<string>, .

automapper, Missing map from String to String., - ToList, - :

admins.Select(a => new AdminUserProfile{
  Id = a.Id,
  Name = a.Name,
  Roles = a.Roles.Select(r => r.Name).ToList()
})

, Linq-to-entity, LINQ to Entities does not recognize the method ToList.

, "" automapper - :

admins.Select(a => new {
  a.Id,
  a.Name,
  Roles = a.Roles.Select(r => r.Name)
})
.AsEnumerable() // run the linq-to-entities query
.Select(a => new AdminUserProfile{
  Id = a.Id,
  Name = a.Name,
  Roles = a.Roles.ToList()
})

, .

AdminUserProfile.Roles IEnumerable<string>.

+6

All Articles