4

I´ve tried a lot, but I can´t find what I´m really looking for. This is my case: I have an EF-Core entity with navigation-properties and a viewModel:

public class SomeEntity
{
    public Guid Id { get; set; }
    public virtual NestedObject NestedObject { get; set; }
    public DateTime Created { get; set; }
    public DateTime Modified { get; set; }
}

public class SomeEntityViewModel
{
    public Guid Id { get; set; }
    public string NestedObjectStringValue { get; set; }
    public int NestedValueIntValue { get; set; }
}

This is my CreateMap which creates a new NestedObject even if no NestedObject-Property is set (Condition doesn´t seem to apply here):

CreateMap<SomeEntityViewModel, SomeEntity>(MemberList.Source)
        .ForPath(m => m.NestedObject.StringValue, opt =>
        {
            opt.Condition(s => s.Destination.NestedObject != null); 
            opt.MapFrom(m => m.NestedObjectStringValue);
        });

This avoids Creating a new NestedObject if Properties are null but if not, the NestedObject Properties are not mapped:

CreateMap<SomeEntityViewModel, SomeEntity>(MemberList.Source)
    .ForMember(m => m.NestedObject, opt => opt.AllowNull());

Second CreateMap doesn´t Map NestedObject-Properties if they are set, first creates a new NestedObject if the Properties are not set. But both together are not working. Any ideas how to solve this?

1
  • Don't use ReverseMap then. Create the reverse map explicitly. Oct 12, 2019 at 13:03

1 Answer 1

5

Remove ReverseMap() ,then try to use AutoMapper Conditional Mapping and use ForPath instead of ForMember for nested child object properties:

CreateMap<SomeEntityViewModel, SomeEntity>()
    .ForPath(
            m => m.NestedObject.StringValue, 
            opt => {                         
                     opt.Condition(
                        s => s.DestinationMember != null && s.DestinationMember != "" 
                     );
                     opt.MapFrom(s => s.NestedObjectStringValue);
                   }
            );

The same to IntValue.

Update

So, if the NestedObject is null, you do not want to to map the value from SomeEntityViewModel to it. If the NestedObject is not null,mapping works.

Please refer to below code which uses AfterMap

CreateMap<SomeEntityViewModel, SomeEntity>()
             .ForMember(q => q.NestedObject, option => option.Ignore())
             .AfterMap((src, dst) => {
                     if(dst.NestedObject != null)
                     {
                     dst.NestedObject.StringValue = src.NestedObjectStringValue;
                     }

                 });
7
  • Thanks a lot for your answer! I‘ll try this asap
    – Joshit
    Oct 14, 2019 at 8:58
  • I want to check NestedObject for null. Your solution checks NestedObject.StringValue, which is not needed. I´ve updated my question to explain better what I´m trying to achieve.
    – Joshit
    Oct 14, 2019 at 9:57
  • @Joshit You could get NestedObject by using s.Destination.NestedObject ,try opt.Condition(s => s.Destination.NestedObject != null);.Not sure it will work..
    – Ryan
    Oct 14, 2019 at 10:07
  • That doesn´t work either. I´ve updated my question again - sorry! It is hard to describe what I want exactly as I´m not a native speaker
    – Joshit
    Oct 14, 2019 at 14:19
  • @Joshit I update my code and hope it is useful.if it is still not you want,could you write some examples of input and result of the mapping to make it clear?
    – Ryan
    Oct 15, 2019 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.