Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
    Kevin Sigmund
    But for some reason, AutoMapper is trying to map the resolver: System.InvalidCastException : Unable to cast object of type 'Newgistics.Fulfillment.AutoStore.Synq.Client.Mappings.ProductImagePathResolver' to type 'Newgistics.Fulfillment.AutoStore.Common.Mappings.NullableValueResolver'.
    Kevin Sigmund
    I ended up just using constructor-injection into the Profile and then passing that into the Resolver there. Still, not sure why I'm seeing the behavior I described.
    Maxime Dubois
    how can I create mapping for the source being a serialized json array to a destination of a list of an object?
    Shayne van Asperen
    @maximedubois Mapper.CreateMap<string, List<MyObject>>() with your own custom value resolver
    Victorio Berra
    Is there a way to specify a global base map without having to include every time?
    For example, I have IAuditable and IAuditableViewModel. Some of my viewmodels have it, some dont. In my profiles I am having to Include<IAuditable, IAuditableViewModel>()every time
    I have two models, one has a List<Guid> collection and one has a List<Dto> collection. When mapping I'd like to resolve those Guids to Dtos but IValueResolver doesn't support async which I need to call certain methods to resolve the objects, how can I accomplish this with AutoMapper?
    Thanks! :)
    Victorio Berra
    ForAllMaps is not running for me. I put throw new NotImplemenetEx... My assert config blows up before that throw ever happens. Shouldnt all profiles be loaded before this happens?
    Looks like order matters. If I put ForAllMaprs directly on the config in AddAutoMapper it works.
    Teo Chai Huat
    Hi All, not sure what I did wrong, my nullable object always reset to default value. how do I configure automapper to ignore/skip null object?

    Guys I need some help.

    I want to set a destination object to null if certain properties in the source are not present. How would that look?


    Another question - is it possible to use AutoMapper to convert another member if it is a subproperty of another mapping?

    Source_2 has a property called Destination_1


    CreateMap<Source_1, Destination_1>();
    CreateMap<Source_2, Destination_2>()
       .ForMember(d => d.Destination_1, o => o.MapFrom(s => map into <Destination_1> using the first mapping));
    Jack Herring
    Unsure if it is bad etiquette here to post links to SO questions but I am stuck and in need of help! https://stackoverflow.com/questions/56258652/automapper-global-configuration-convert-null-array-property-to-empty-array-by does anyone know how to configure default array conversion behaviour when the source array is null? AutoMapper is exploding because int[] doesn't have a no-arg ctor
    Bob Meijwaard
    Mapper.Initialize(cfg => { cfg.AddProfile<CoreProfile>(); }); So as I just updated to v8.1.1 and VS now tells me that the static API is obsolute and that I have to use DI. Why? I don't want that, please do not obsolute the static API.
    You should do the following:
    var cfg = new MapperConfigurationExpression();
    cfg.CreateMap<Domain.Entities.OrderRoot, IApp.DTO.OrderRoot>();
    @DinoSourcesRex, if I understand you requirement propertly then you may try something like this
    cfg.CreateMap<Source1, Destination1>();
    cfg.CreateMap<Source2, Destination1>()
          .IncludeMembers(x => x.PropOfSource1Type);
    class Source2
      Source1 PropOfSource1Type {get; set;}
      //… other props
    I've encountered an issue which seems to have been raised previously (AutoMapper/AutoMapper#2745) but with no resolution, and the workaround mentioned has a flaw when the destination object has more than one constructor.
    I've looked at the source code in order to type to identify a solution, but it seems that during the MapDestinationCtorToSource method, the only access to the ForCtor methods in via:
    options._typeMapConfigs[].CtorParamConfigurations[], but due to the generic abstraction layers involved, I can't fathom how to do the checking required.
    Any assistance would be appreciated...
    James Webster
    Is it possible to customise the mapping of a class -> ExpandoObject? I have an IEnumerable<SomeClass> that I can map to IEnumerable<ExpandoObject> just fine, but I want to explictly override some of the dynamically generated mapping, (e.g. some properties on SomeClass should be given a different name on the ExpandoObject, some should have their type converted, but most of them should be mapped automatically).
    If I just call _mapper.Map<IEnumerable<ExpandoObject>>(anEnumerableOfMyClass) then everything is mapped onto the enumerable of ExpandoObject.
    James Webster
    If I configure a map via CreateMap<SomeClass, ExpandoObject>() then I get a list of empty ExpandoObjects. Is there a way to tell CreateMap to do the default mapping (I have tried various combinations of .ForAllMembers() but there doesn't appear to be a way to say - just do the default behaviour... which I would then override with .ForMember()
    James Webster
    I think the problem I have here is that mapping is generally focused on the members of the destination. In this case, the destination has no explicit members. But there doesn't seem to be a way to override the default behaviour when mapping TO dynamic.
    Hello there, Are there anyone who know what encoding type of test code files(AutoMapper.UnitTests.Internationalization.When_mapping_a_source_with_non_english_property_names, AutoMapper.UnitTests.Tests.StubNamingConvention)? I couldn't build and test automapper solution, My jetbrains rider can't recognize encoding type of this file.
    Osama AbuSitta
    hello there , I am download automapper source code and trying to build it ,
    when run the psake.cmd I got this error "C:\Users\o.abusitta.nuget\packages\microsoft.dotnet.apicompat\1.0.0-beta.19423.1\build\Microsoft.DotNet.ApiCompat.targets(96,5): error : ApiCompat failed for 'D:\AutoMapper-master\AutoMapper-master\src\AutoMapper\bin\Debug\netstandard2.0\AutoMapper.dll' [D:\AutoMapper-master\AutoMapper-master\src\AutoMapper\AutoMapper.csproj]"
    and when I build it i got "Error ApiCompat failed for '"
    There is any guideline to build the solution
    Osama AbuSitta
    Edward Lichtman
    I'm looking for a "<Any,Any>" method of mapping. I can't seem to find one and was wondering if there is a "best-practices" reason there isn't one, and does anyone have a workaround like a "CatchAllMapper" implementation?
    Sander Geerts

    Hi all, how would I configure Automapper in a Profile to do a mapping which changes the object struture? e.g.:


    Should Become

            List<ChildObject> // ChildObject A and B
            List<ChildObject> // ChildObject C
    this.CreateMap<Data.Task.Task, POCO.Task>() .ForMember(d => d.User, o => o.MapFrom(m => m.UserTaskAssignment.FirstOrDefault().User.Name))
    I am using automapper to map my data object to poco. User property is not being populated even though I added mapping. Rest of the map works.
    I tried .AfterMap((o,d) => d.User = o.UserTaskAssignment.FirstOrDefault()?.User.Name); as well. Did not work.
    Daxesh Vora

    I am trying to map Automapper to map string to datetime and datetime? so using below method

    "CreateMap<DateTime?, string>().ConvertUsing(opt => opt.ConvertDateTimeToString("dd/MM/yyyy"));
    CreateMap<string, DateTime?>().ConvertUsing(opt => opt.ConvertStringToNullableDateTime("dd/MM/yyyy"));
    CreateMap<DateTime, string>().ConvertUsing(opt => opt.Date.ConvertDateTimeToString("dd/MM/yyyy"));
    CreateMap<string, DateTime>().ConvertUsing(opt => opt.ConvertStringToDateTime("dd/MM/yyyy"));"

    But its giving below error

    InvalidCastException: Unable to cast object of type 'System.Func4[System.String,System.Nullable1[System.DateTime],AutoMapper.ResolutionContext,System.DateTime]' to type 'System.Func4[System.String,System.Nullable1[System.DateTime],AutoMapper.ResolutionContext,System.Nullable`1[System.DateTime]]'.
    AutoMapper.MapperConfiguration.GetMapperFunc<TSource, TDestination>(MapRequest mapRequest)

    AutoMapperMappingException: Error mapping types.

    Do you need an automapper map for DateTime? to string or string to DateTime?. I think if you are mapping object property you would have code there to cast it. String to DateTime is tricky. Need to use DateTime.TryParse.
    Daxesh Vora
    I know I can do for each object property. But there were around 20 datetime property and so I wanted to manage it using less code instead of typing for all 20 properties. Finally I understood the issue, it was not able to understand when converting from string to datetime to use conversion of datetime or datetime?. On removing one function of string datetime, it worked fine.
    Hi, I have run into an issue that has got me stumped. I have posted the question on StackOverflow if there is anyone here who might be able to help me :) https://stackoverflow.com/questions/61721294/in-automapper-how-does-one-project-from-an-abstract-base-class-to-an-interface
    I am running into an error stating that my interface doesn't have a default constructor (obviously). I have read all of the documentation and reviewed the unit tests and am really stuck.
    Well it looks like it isn't possible, which is a shame. The response from issue AutoMapper/AutoMapper#3293 is that it is due to a limitation of the EF provider. But in this case the EF provider does allow it. I assume this is a common issue with people and I am wondering what workarounds people have devised?
    Sami Al Khatib
    Just out of curiosity, why would you map entities to interfaces?
    Anyone awake? I'm trying to map an object with a T[] property to an object with an ObservableCollection<T> getter-only property, where both properties have the same name. The property isn't mapped because the destination is getter-only. But if I reluctantly add a setter to the ObservableCollection property, Automapper throws an ArgumentException "Argument types do not match". What gives?
    I get this same error even if I manually map the member using a custom resolver :\
    I found a workaround: replacing this call
     var foos = mapper.ProjectTo<FooModel>(myFoos.AsQueryable());
    with this:
     var foos = myFoos.Select(x => mapper.Map<FooModel>(x));
    Not really sure why the result is different? Maybe I'm misunderstanding ProjectTo()?
    Kevin Hernández Rostrán

    Hello everyone. I have a problem
    I have this map

    CreateMap<RemoFlatProcedureDTO, string>()
                    .ForMember(destination => destination, option => option.MapFrom(source => source.procedureTemplate.code));

    but i get this message:

    Automapper Custom configuration for members is only supported for top-level individual members on a type

    Have you some suggestions? How can I map to plain string value?
    Kevin Hernández Rostrán
    I got it... Thank you!
    CreateMap<RemoFlatProcedureDTO, string>()
                                    .ConstructUsing(source => source.procedureTemplate.code);