Automapper - need to map a parent property when mapping children












1















The basic problem is that I have properties on my source parent entities that I need to map to my destination child DTOs when the child entities are mapped to the destination child DTOs. The child entities do not have navigation properties to their parent. As you can see below, the parent has a field of the child type. You can see in the code below that ParentSource has a property called TitleImage of type ImageSource. So linkage is one directional. In my code, I use the ImageSource everywhere. There are at least 15 different entities with properties of type ImageSource. I normalized my database. The ImageSource entity does not have X and Y coordinates because only a few, maybe 5 out of 30, places actually need those extra values. In those few places, I added the X and Y on the parent as you see on the ParentSource class with the TitleImageX and TitleImageY.



I could easily do what I want with a loop, and that is what I am doing now, but I would love to use Automapper for this if I could. It would be way less code.



Here are my classes:



public class ParentSource
{
public string Id { get; set; }
public string Name { get; set; }
public ImageSource HomeImage { get; set; }
public ImageSource TitleImage { get; set; }
//These should be copied to the child object
public int TitleImageX { get; set; }
public int TitleImageY { get; set; }
}
public class ParentDest
{
public string ParentId { get; set; }
public string DisplayName { get; set; }
public ImageDest HomeImage { get; set; }
public ImageDest TitleImage { get; set; }
}
public class ImageSource
{
public string Id { get; set; }
public string Url { get; set; }
public decimal Height { get; set; }
public decimal Width { get; set; }
}
public class ImageDest
{
public string ImageId { get; set; }
public string ImageUrl { get; set; }
public decimal Height { get; set; }
public decimal Width { get; set; }
//Not all images have Coordinate
public decimal XCoordinate { get; set; }
public decimal YCoordinate { get; set; }
}


Here are what I have so far for doing the mappings. I don't know how to copy the X & Y coordinates from the parent to the child.



        CreateMap<ParentSource, ParentDest>()
.ForMember(
dest => dest.ParentId,
opt => opt.MapFrom(src => src.Id))
.ForMember(
dest => dest.DisplayName,
opt => opt.MapFrom(src => src.Name));

CreateMap<ImageSource, ImageDest>()
.ForMember(
dest => dest.ImageId,
opt => opt.MapFrom(src => src.Id))
.ForMember(
dest => dest.ImageUrl,
opt => opt.MapFrom(src => src.Url));


Thank you for your time and help.










share|improve this question

























  • docs.automapper.org/en/latest/…

    – Lucian Bargaoanu
    Dec 29 '18 at 5:14











  • Can you give an example? I have looked at ForPath and I don't see how it will work.

    – Bobby Ortiz
    Dec 31 '18 at 14:46











  • I've already linked to examples.

    – Lucian Bargaoanu
    Dec 31 '18 at 14:50











  • Thanks again. I followed the link you posted, and it took me to these examples: github.com/AutoMapper/AutoMapper/…

    – Bobby Ortiz
    Dec 31 '18 at 19:20











  • However, i don't see how any of these solves my problem.

    – Bobby Ortiz
    Dec 31 '18 at 19:20
















1















The basic problem is that I have properties on my source parent entities that I need to map to my destination child DTOs when the child entities are mapped to the destination child DTOs. The child entities do not have navigation properties to their parent. As you can see below, the parent has a field of the child type. You can see in the code below that ParentSource has a property called TitleImage of type ImageSource. So linkage is one directional. In my code, I use the ImageSource everywhere. There are at least 15 different entities with properties of type ImageSource. I normalized my database. The ImageSource entity does not have X and Y coordinates because only a few, maybe 5 out of 30, places actually need those extra values. In those few places, I added the X and Y on the parent as you see on the ParentSource class with the TitleImageX and TitleImageY.



I could easily do what I want with a loop, and that is what I am doing now, but I would love to use Automapper for this if I could. It would be way less code.



Here are my classes:



public class ParentSource
{
public string Id { get; set; }
public string Name { get; set; }
public ImageSource HomeImage { get; set; }
public ImageSource TitleImage { get; set; }
//These should be copied to the child object
public int TitleImageX { get; set; }
public int TitleImageY { get; set; }
}
public class ParentDest
{
public string ParentId { get; set; }
public string DisplayName { get; set; }
public ImageDest HomeImage { get; set; }
public ImageDest TitleImage { get; set; }
}
public class ImageSource
{
public string Id { get; set; }
public string Url { get; set; }
public decimal Height { get; set; }
public decimal Width { get; set; }
}
public class ImageDest
{
public string ImageId { get; set; }
public string ImageUrl { get; set; }
public decimal Height { get; set; }
public decimal Width { get; set; }
//Not all images have Coordinate
public decimal XCoordinate { get; set; }
public decimal YCoordinate { get; set; }
}


Here are what I have so far for doing the mappings. I don't know how to copy the X & Y coordinates from the parent to the child.



        CreateMap<ParentSource, ParentDest>()
.ForMember(
dest => dest.ParentId,
opt => opt.MapFrom(src => src.Id))
.ForMember(
dest => dest.DisplayName,
opt => opt.MapFrom(src => src.Name));

CreateMap<ImageSource, ImageDest>()
.ForMember(
dest => dest.ImageId,
opt => opt.MapFrom(src => src.Id))
.ForMember(
dest => dest.ImageUrl,
opt => opt.MapFrom(src => src.Url));


Thank you for your time and help.










share|improve this question

























  • docs.automapper.org/en/latest/…

    – Lucian Bargaoanu
    Dec 29 '18 at 5:14











  • Can you give an example? I have looked at ForPath and I don't see how it will work.

    – Bobby Ortiz
    Dec 31 '18 at 14:46











  • I've already linked to examples.

    – Lucian Bargaoanu
    Dec 31 '18 at 14:50











  • Thanks again. I followed the link you posted, and it took me to these examples: github.com/AutoMapper/AutoMapper/…

    – Bobby Ortiz
    Dec 31 '18 at 19:20











  • However, i don't see how any of these solves my problem.

    – Bobby Ortiz
    Dec 31 '18 at 19:20














1












1








1








The basic problem is that I have properties on my source parent entities that I need to map to my destination child DTOs when the child entities are mapped to the destination child DTOs. The child entities do not have navigation properties to their parent. As you can see below, the parent has a field of the child type. You can see in the code below that ParentSource has a property called TitleImage of type ImageSource. So linkage is one directional. In my code, I use the ImageSource everywhere. There are at least 15 different entities with properties of type ImageSource. I normalized my database. The ImageSource entity does not have X and Y coordinates because only a few, maybe 5 out of 30, places actually need those extra values. In those few places, I added the X and Y on the parent as you see on the ParentSource class with the TitleImageX and TitleImageY.



I could easily do what I want with a loop, and that is what I am doing now, but I would love to use Automapper for this if I could. It would be way less code.



Here are my classes:



public class ParentSource
{
public string Id { get; set; }
public string Name { get; set; }
public ImageSource HomeImage { get; set; }
public ImageSource TitleImage { get; set; }
//These should be copied to the child object
public int TitleImageX { get; set; }
public int TitleImageY { get; set; }
}
public class ParentDest
{
public string ParentId { get; set; }
public string DisplayName { get; set; }
public ImageDest HomeImage { get; set; }
public ImageDest TitleImage { get; set; }
}
public class ImageSource
{
public string Id { get; set; }
public string Url { get; set; }
public decimal Height { get; set; }
public decimal Width { get; set; }
}
public class ImageDest
{
public string ImageId { get; set; }
public string ImageUrl { get; set; }
public decimal Height { get; set; }
public decimal Width { get; set; }
//Not all images have Coordinate
public decimal XCoordinate { get; set; }
public decimal YCoordinate { get; set; }
}


Here are what I have so far for doing the mappings. I don't know how to copy the X & Y coordinates from the parent to the child.



        CreateMap<ParentSource, ParentDest>()
.ForMember(
dest => dest.ParentId,
opt => opt.MapFrom(src => src.Id))
.ForMember(
dest => dest.DisplayName,
opt => opt.MapFrom(src => src.Name));

CreateMap<ImageSource, ImageDest>()
.ForMember(
dest => dest.ImageId,
opt => opt.MapFrom(src => src.Id))
.ForMember(
dest => dest.ImageUrl,
opt => opt.MapFrom(src => src.Url));


Thank you for your time and help.










share|improve this question
















The basic problem is that I have properties on my source parent entities that I need to map to my destination child DTOs when the child entities are mapped to the destination child DTOs. The child entities do not have navigation properties to their parent. As you can see below, the parent has a field of the child type. You can see in the code below that ParentSource has a property called TitleImage of type ImageSource. So linkage is one directional. In my code, I use the ImageSource everywhere. There are at least 15 different entities with properties of type ImageSource. I normalized my database. The ImageSource entity does not have X and Y coordinates because only a few, maybe 5 out of 30, places actually need those extra values. In those few places, I added the X and Y on the parent as you see on the ParentSource class with the TitleImageX and TitleImageY.



I could easily do what I want with a loop, and that is what I am doing now, but I would love to use Automapper for this if I could. It would be way less code.



Here are my classes:



public class ParentSource
{
public string Id { get; set; }
public string Name { get; set; }
public ImageSource HomeImage { get; set; }
public ImageSource TitleImage { get; set; }
//These should be copied to the child object
public int TitleImageX { get; set; }
public int TitleImageY { get; set; }
}
public class ParentDest
{
public string ParentId { get; set; }
public string DisplayName { get; set; }
public ImageDest HomeImage { get; set; }
public ImageDest TitleImage { get; set; }
}
public class ImageSource
{
public string Id { get; set; }
public string Url { get; set; }
public decimal Height { get; set; }
public decimal Width { get; set; }
}
public class ImageDest
{
public string ImageId { get; set; }
public string ImageUrl { get; set; }
public decimal Height { get; set; }
public decimal Width { get; set; }
//Not all images have Coordinate
public decimal XCoordinate { get; set; }
public decimal YCoordinate { get; set; }
}


Here are what I have so far for doing the mappings. I don't know how to copy the X & Y coordinates from the parent to the child.



        CreateMap<ParentSource, ParentDest>()
.ForMember(
dest => dest.ParentId,
opt => opt.MapFrom(src => src.Id))
.ForMember(
dest => dest.DisplayName,
opt => opt.MapFrom(src => src.Name));

CreateMap<ImageSource, ImageDest>()
.ForMember(
dest => dest.ImageId,
opt => opt.MapFrom(src => src.Id))
.ForMember(
dest => dest.ImageUrl,
opt => opt.MapFrom(src => src.Url));


Thank you for your time and help.







automapper automapping automapper-6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 14:36







Bobby Ortiz

















asked Dec 28 '18 at 17:34









Bobby OrtizBobby Ortiz

2,00752737




2,00752737













  • docs.automapper.org/en/latest/…

    – Lucian Bargaoanu
    Dec 29 '18 at 5:14











  • Can you give an example? I have looked at ForPath and I don't see how it will work.

    – Bobby Ortiz
    Dec 31 '18 at 14:46











  • I've already linked to examples.

    – Lucian Bargaoanu
    Dec 31 '18 at 14:50











  • Thanks again. I followed the link you posted, and it took me to these examples: github.com/AutoMapper/AutoMapper/…

    – Bobby Ortiz
    Dec 31 '18 at 19:20











  • However, i don't see how any of these solves my problem.

    – Bobby Ortiz
    Dec 31 '18 at 19:20



















  • docs.automapper.org/en/latest/…

    – Lucian Bargaoanu
    Dec 29 '18 at 5:14











  • Can you give an example? I have looked at ForPath and I don't see how it will work.

    – Bobby Ortiz
    Dec 31 '18 at 14:46











  • I've already linked to examples.

    – Lucian Bargaoanu
    Dec 31 '18 at 14:50











  • Thanks again. I followed the link you posted, and it took me to these examples: github.com/AutoMapper/AutoMapper/…

    – Bobby Ortiz
    Dec 31 '18 at 19:20











  • However, i don't see how any of these solves my problem.

    – Bobby Ortiz
    Dec 31 '18 at 19:20

















docs.automapper.org/en/latest/…

– Lucian Bargaoanu
Dec 29 '18 at 5:14





docs.automapper.org/en/latest/…

– Lucian Bargaoanu
Dec 29 '18 at 5:14













Can you give an example? I have looked at ForPath and I don't see how it will work.

– Bobby Ortiz
Dec 31 '18 at 14:46





Can you give an example? I have looked at ForPath and I don't see how it will work.

– Bobby Ortiz
Dec 31 '18 at 14:46













I've already linked to examples.

– Lucian Bargaoanu
Dec 31 '18 at 14:50





I've already linked to examples.

– Lucian Bargaoanu
Dec 31 '18 at 14:50













Thanks again. I followed the link you posted, and it took me to these examples: github.com/AutoMapper/AutoMapper/…

– Bobby Ortiz
Dec 31 '18 at 19:20





Thanks again. I followed the link you posted, and it took me to these examples: github.com/AutoMapper/AutoMapper/…

– Bobby Ortiz
Dec 31 '18 at 19:20













However, i don't see how any of these solves my problem.

– Bobby Ortiz
Dec 31 '18 at 19:20





However, i don't see how any of these solves my problem.

– Bobby Ortiz
Dec 31 '18 at 19:20












2 Answers
2






active

oldest

votes


















0














Or you can do it in config (not tried or tested) which you may prefer, but there is no check for null on TitleImage



CreateMap<ParentSource, ParentDest>()
.ForMember(
dest => dest.ParentId,
opt => opt.MapFrom(src => src.Id))
.ForMember(
dest => dest.DisplayName,
opt => opt.MapFrom(src => src.Name))
.ForMember(dest=>dest.TitleImage,
opt=>opt.MapFrom(src=>new ImageDest
{
Width = src.TitleImage.Width,
Height = src.TitleImage.Height,
ImageId = src.TitleImage.Id,
ImageUrl = src.TitleImage.Url,
XCoordinate = src.TitleImageX,
YCoordinate = src.TitleImageY
}
));





share|improve this answer































    -1














    From what I gather you want the X&Y from the parent into the child. You can use a converter class for this.



    Converter:



    public class ParentConverter : ITypeConverter<ParentSource, ParentDest>
    {
    public ParentDest Convert(ParentSource source, ParentDest destination, ResolutionContext context)
    {
    if (destination == null)
    destination = new ParentDest();

    destination.DisplayName = source.Name;
    destination.ParentId = source.Id;

    if (source.TitleImage != null)
    {
    destination.TitleImage = context.Mapper.Map<ImageDest>(source.TitleImage);
    destination.TitleImage.XCoordinate = source.TitleImageX;
    destination.TitleImage.YCoordinate = source.TitleImageY;
    }

    return destination;
    }
    }


    Mapping:



    CreateMap<ParentSource, ParentDest>()
    .ConvertUsing<ParentConverter>();

    CreateMap<ImageSource, ImageDest>()
    .ForMember(
    dest => dest.ImageId,
    opt => opt.MapFrom(src => src.Id))
    .ForMember(
    dest => dest.ImageUrl,
    opt => opt.MapFrom(src => src.Url);
    });





    share|improve this answer


























    • Yes, but that's not exactly auto, is it? :)

      – Lucian Bargaoanu
      Jan 4 at 15:45











    • I must have missed your 'auto' version of the answer!

      – matt_lethargic
      Jan 4 at 16:16











    • ForPath would do it.

      – Lucian Bargaoanu
      Jan 4 at 16:56











    • Why not post the answer then with the worked out example for this particular question, then everyone can learn and the OP will have a nice solution? Or you could just keep posting links to the docs!

      – matt_lethargic
      Jan 4 at 17:36











    • Teach a man to fish...

      – Lucian Bargaoanu
      Jan 4 at 17:50











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962241%2fautomapper-need-to-map-a-parent-property-when-mapping-children%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Or you can do it in config (not tried or tested) which you may prefer, but there is no check for null on TitleImage



    CreateMap<ParentSource, ParentDest>()
    .ForMember(
    dest => dest.ParentId,
    opt => opt.MapFrom(src => src.Id))
    .ForMember(
    dest => dest.DisplayName,
    opt => opt.MapFrom(src => src.Name))
    .ForMember(dest=>dest.TitleImage,
    opt=>opt.MapFrom(src=>new ImageDest
    {
    Width = src.TitleImage.Width,
    Height = src.TitleImage.Height,
    ImageId = src.TitleImage.Id,
    ImageUrl = src.TitleImage.Url,
    XCoordinate = src.TitleImageX,
    YCoordinate = src.TitleImageY
    }
    ));





    share|improve this answer




























      0














      Or you can do it in config (not tried or tested) which you may prefer, but there is no check for null on TitleImage



      CreateMap<ParentSource, ParentDest>()
      .ForMember(
      dest => dest.ParentId,
      opt => opt.MapFrom(src => src.Id))
      .ForMember(
      dest => dest.DisplayName,
      opt => opt.MapFrom(src => src.Name))
      .ForMember(dest=>dest.TitleImage,
      opt=>opt.MapFrom(src=>new ImageDest
      {
      Width = src.TitleImage.Width,
      Height = src.TitleImage.Height,
      ImageId = src.TitleImage.Id,
      ImageUrl = src.TitleImage.Url,
      XCoordinate = src.TitleImageX,
      YCoordinate = src.TitleImageY
      }
      ));





      share|improve this answer


























        0












        0








        0







        Or you can do it in config (not tried or tested) which you may prefer, but there is no check for null on TitleImage



        CreateMap<ParentSource, ParentDest>()
        .ForMember(
        dest => dest.ParentId,
        opt => opt.MapFrom(src => src.Id))
        .ForMember(
        dest => dest.DisplayName,
        opt => opt.MapFrom(src => src.Name))
        .ForMember(dest=>dest.TitleImage,
        opt=>opt.MapFrom(src=>new ImageDest
        {
        Width = src.TitleImage.Width,
        Height = src.TitleImage.Height,
        ImageId = src.TitleImage.Id,
        ImageUrl = src.TitleImage.Url,
        XCoordinate = src.TitleImageX,
        YCoordinate = src.TitleImageY
        }
        ));





        share|improve this answer













        Or you can do it in config (not tried or tested) which you may prefer, but there is no check for null on TitleImage



        CreateMap<ParentSource, ParentDest>()
        .ForMember(
        dest => dest.ParentId,
        opt => opt.MapFrom(src => src.Id))
        .ForMember(
        dest => dest.DisplayName,
        opt => opt.MapFrom(src => src.Name))
        .ForMember(dest=>dest.TitleImage,
        opt=>opt.MapFrom(src=>new ImageDest
        {
        Width = src.TitleImage.Width,
        Height = src.TitleImage.Height,
        ImageId = src.TitleImage.Id,
        ImageUrl = src.TitleImage.Url,
        XCoordinate = src.TitleImageX,
        YCoordinate = src.TitleImageY
        }
        ));






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 16:15









        matt_lethargicmatt_lethargic

        2,09511226




        2,09511226

























            -1














            From what I gather you want the X&Y from the parent into the child. You can use a converter class for this.



            Converter:



            public class ParentConverter : ITypeConverter<ParentSource, ParentDest>
            {
            public ParentDest Convert(ParentSource source, ParentDest destination, ResolutionContext context)
            {
            if (destination == null)
            destination = new ParentDest();

            destination.DisplayName = source.Name;
            destination.ParentId = source.Id;

            if (source.TitleImage != null)
            {
            destination.TitleImage = context.Mapper.Map<ImageDest>(source.TitleImage);
            destination.TitleImage.XCoordinate = source.TitleImageX;
            destination.TitleImage.YCoordinate = source.TitleImageY;
            }

            return destination;
            }
            }


            Mapping:



            CreateMap<ParentSource, ParentDest>()
            .ConvertUsing<ParentConverter>();

            CreateMap<ImageSource, ImageDest>()
            .ForMember(
            dest => dest.ImageId,
            opt => opt.MapFrom(src => src.Id))
            .ForMember(
            dest => dest.ImageUrl,
            opt => opt.MapFrom(src => src.Url);
            });





            share|improve this answer


























            • Yes, but that's not exactly auto, is it? :)

              – Lucian Bargaoanu
              Jan 4 at 15:45











            • I must have missed your 'auto' version of the answer!

              – matt_lethargic
              Jan 4 at 16:16











            • ForPath would do it.

              – Lucian Bargaoanu
              Jan 4 at 16:56











            • Why not post the answer then with the worked out example for this particular question, then everyone can learn and the OP will have a nice solution? Or you could just keep posting links to the docs!

              – matt_lethargic
              Jan 4 at 17:36











            • Teach a man to fish...

              – Lucian Bargaoanu
              Jan 4 at 17:50
















            -1














            From what I gather you want the X&Y from the parent into the child. You can use a converter class for this.



            Converter:



            public class ParentConverter : ITypeConverter<ParentSource, ParentDest>
            {
            public ParentDest Convert(ParentSource source, ParentDest destination, ResolutionContext context)
            {
            if (destination == null)
            destination = new ParentDest();

            destination.DisplayName = source.Name;
            destination.ParentId = source.Id;

            if (source.TitleImage != null)
            {
            destination.TitleImage = context.Mapper.Map<ImageDest>(source.TitleImage);
            destination.TitleImage.XCoordinate = source.TitleImageX;
            destination.TitleImage.YCoordinate = source.TitleImageY;
            }

            return destination;
            }
            }


            Mapping:



            CreateMap<ParentSource, ParentDest>()
            .ConvertUsing<ParentConverter>();

            CreateMap<ImageSource, ImageDest>()
            .ForMember(
            dest => dest.ImageId,
            opt => opt.MapFrom(src => src.Id))
            .ForMember(
            dest => dest.ImageUrl,
            opt => opt.MapFrom(src => src.Url);
            });





            share|improve this answer


























            • Yes, but that's not exactly auto, is it? :)

              – Lucian Bargaoanu
              Jan 4 at 15:45











            • I must have missed your 'auto' version of the answer!

              – matt_lethargic
              Jan 4 at 16:16











            • ForPath would do it.

              – Lucian Bargaoanu
              Jan 4 at 16:56











            • Why not post the answer then with the worked out example for this particular question, then everyone can learn and the OP will have a nice solution? Or you could just keep posting links to the docs!

              – matt_lethargic
              Jan 4 at 17:36











            • Teach a man to fish...

              – Lucian Bargaoanu
              Jan 4 at 17:50














            -1












            -1








            -1







            From what I gather you want the X&Y from the parent into the child. You can use a converter class for this.



            Converter:



            public class ParentConverter : ITypeConverter<ParentSource, ParentDest>
            {
            public ParentDest Convert(ParentSource source, ParentDest destination, ResolutionContext context)
            {
            if (destination == null)
            destination = new ParentDest();

            destination.DisplayName = source.Name;
            destination.ParentId = source.Id;

            if (source.TitleImage != null)
            {
            destination.TitleImage = context.Mapper.Map<ImageDest>(source.TitleImage);
            destination.TitleImage.XCoordinate = source.TitleImageX;
            destination.TitleImage.YCoordinate = source.TitleImageY;
            }

            return destination;
            }
            }


            Mapping:



            CreateMap<ParentSource, ParentDest>()
            .ConvertUsing<ParentConverter>();

            CreateMap<ImageSource, ImageDest>()
            .ForMember(
            dest => dest.ImageId,
            opt => opt.MapFrom(src => src.Id))
            .ForMember(
            dest => dest.ImageUrl,
            opt => opt.MapFrom(src => src.Url);
            });





            share|improve this answer















            From what I gather you want the X&Y from the parent into the child. You can use a converter class for this.



            Converter:



            public class ParentConverter : ITypeConverter<ParentSource, ParentDest>
            {
            public ParentDest Convert(ParentSource source, ParentDest destination, ResolutionContext context)
            {
            if (destination == null)
            destination = new ParentDest();

            destination.DisplayName = source.Name;
            destination.ParentId = source.Id;

            if (source.TitleImage != null)
            {
            destination.TitleImage = context.Mapper.Map<ImageDest>(source.TitleImage);
            destination.TitleImage.XCoordinate = source.TitleImageX;
            destination.TitleImage.YCoordinate = source.TitleImageY;
            }

            return destination;
            }
            }


            Mapping:



            CreateMap<ParentSource, ParentDest>()
            .ConvertUsing<ParentConverter>();

            CreateMap<ImageSource, ImageDest>()
            .ForMember(
            dest => dest.ImageId,
            opt => opt.MapFrom(src => src.Id))
            .ForMember(
            dest => dest.ImageUrl,
            opt => opt.MapFrom(src => src.Url);
            });






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 4 at 16:08

























            answered Jan 4 at 11:37









            matt_lethargicmatt_lethargic

            2,09511226




            2,09511226













            • Yes, but that's not exactly auto, is it? :)

              – Lucian Bargaoanu
              Jan 4 at 15:45











            • I must have missed your 'auto' version of the answer!

              – matt_lethargic
              Jan 4 at 16:16











            • ForPath would do it.

              – Lucian Bargaoanu
              Jan 4 at 16:56











            • Why not post the answer then with the worked out example for this particular question, then everyone can learn and the OP will have a nice solution? Or you could just keep posting links to the docs!

              – matt_lethargic
              Jan 4 at 17:36











            • Teach a man to fish...

              – Lucian Bargaoanu
              Jan 4 at 17:50



















            • Yes, but that's not exactly auto, is it? :)

              – Lucian Bargaoanu
              Jan 4 at 15:45











            • I must have missed your 'auto' version of the answer!

              – matt_lethargic
              Jan 4 at 16:16











            • ForPath would do it.

              – Lucian Bargaoanu
              Jan 4 at 16:56











            • Why not post the answer then with the worked out example for this particular question, then everyone can learn and the OP will have a nice solution? Or you could just keep posting links to the docs!

              – matt_lethargic
              Jan 4 at 17:36











            • Teach a man to fish...

              – Lucian Bargaoanu
              Jan 4 at 17:50

















            Yes, but that's not exactly auto, is it? :)

            – Lucian Bargaoanu
            Jan 4 at 15:45





            Yes, but that's not exactly auto, is it? :)

            – Lucian Bargaoanu
            Jan 4 at 15:45













            I must have missed your 'auto' version of the answer!

            – matt_lethargic
            Jan 4 at 16:16





            I must have missed your 'auto' version of the answer!

            – matt_lethargic
            Jan 4 at 16:16













            ForPath would do it.

            – Lucian Bargaoanu
            Jan 4 at 16:56





            ForPath would do it.

            – Lucian Bargaoanu
            Jan 4 at 16:56













            Why not post the answer then with the worked out example for this particular question, then everyone can learn and the OP will have a nice solution? Or you could just keep posting links to the docs!

            – matt_lethargic
            Jan 4 at 17:36





            Why not post the answer then with the worked out example for this particular question, then everyone can learn and the OP will have a nice solution? Or you could just keep posting links to the docs!

            – matt_lethargic
            Jan 4 at 17:36













            Teach a man to fish...

            – Lucian Bargaoanu
            Jan 4 at 17:50





            Teach a man to fish...

            – Lucian Bargaoanu
            Jan 4 at 17:50


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962241%2fautomapper-need-to-map-a-parent-property-when-mapping-children%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]