【问题标题】:How to preserve the not mapped properties of a collection in a destination object with Automapper?如何使用 Automapper 在目标对象中保留集合的未映射属性?
【发布时间】:2022-12-07 18:59:13
【问题描述】:

初始情况:我有一个源对象和一个目标对象,并且都有一个 Elements 的集合。使用 Automapper,我将源映射到目标对象。之后,我将信息添加到仅存在于目标对象(ItemNameItemNumber)上的属性中。此外,我将信息添加到集合AssetElementDto 中对象的属性Text。之后,我调用mapper.Map(source_update, destination); 来更新目标对象。

问题:当我运行代码时,更新后保留了ItemNameItemNumber的信息。我该怎么做才能同时保留TextAssetElementDto 的信息?

using AutoMapper;
using System.Runtime.InteropServices;

public class Program
{
    static void Main(string[] arg)
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Source, Destination>().PreserveReferences();
            cfg.CreateMap<AssetElement, AssetElementDto>().PreserveReferences();
        });
        var mapper = new Mapper(config);

        var source = new Source()
        {
            Name = "Sinonis",
            Id = new Random().Next(),
            Elements = new List<AssetElement>
            {
                new(){ Id = 1},
                new(){ Id = 2}
            }
        };

        var destination = mapper.Map<Destination>(source);
        destination.ItemNumber = 42;
        destination.Elements.ForEach(e => e.Text ="Wow");

        destination.ItemName = "NPC21";

        var source_update = new Source()
        {
            Name = "Nindalf",
            Id = new Random().Next(),
            Elements = new List<AssetElement>
            {
                new(){ Id = 3},
                new(){ Id = 4}
            }
        };

        Console.WriteLine($"Before update is: {destination.Name} {destination.Id} {destination.ItemNumber} {destination.ItemName}");
        destination.Elements.ForEach(e => Console.WriteLine($" {e.Id} {e.Text}"));

        mapper.Map(source_update, destination);

        Console.WriteLine($"After update is : {destination.Name} {destination.Id} {destination.ItemNumber} {destination.ItemName}");
        destination.Elements.ForEach(e => Console.WriteLine($" {e.Id} {e.Text}"));
    }
}

#region Source

public class Source
{
    public string Name;
    public int Id;

    public List<AssetElement> Elements;

}

public class AssetElement
{
    public int Id;
}

#endregion

#region Destination

public class Destination
{
    public string Name;
    public int Id;

    public List<AssetElementDto> Elements;

    public int ItemNumber;
    public string ItemName;
}

public class AssetElementDto
{
    public string Text;
    public int Id;
}

#endregion

控制台输出:

更新后不会保留“哇”。

Before update is: Sinonis 1643275093 42 NPC21
 1 Wow
 2 Wow
After update is : Nindalf 75522068 42 NPC21
 3 null
 4 null

【问题讨论】:

  • 研究 AutoMapper.Collection。

标签: c# entity-framework .net-core automapper


【解决方案1】:

感谢 Lucian Bargaoanu 这个简短但有力的提示。这解决了我的问题。请在下面的代码中查看必要的更改。这是 AutoMapper.Collection 的 link

using AutoMapper;
using AutoMapper.EquivalencyExpression; //add using

public class Program
{
    static void Main(string[] arg)
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.AddCollectionMappers(); //add the collection mapper
            cfg.CreateMap<Source, Destination>();
            cfg.CreateMap<AssetElement, AssetElementDto>().EqualityComparison((odto, o) => odto.Id == o.Id); //add the equality compersion
        });
        var mapper = new Mapper(config);

        var source = new Source()
        {
            Name = "Sinonis",
            Id = new Random().Next(),
            Elements = new List<AssetElement>
            {
                new(){ Id = 1},
                new(){ Id = 2}
            }
        };

        var destination = mapper.Map<Destination>(source);
        destination.ItemNumber = 42;
        destination.ItemName = "NPC21";
        destination.Elements.ForEach(e => e.Text ="Wow");

       

        var source_update = new Source()
        {
            Name = "Nindalf",
            Id = new Random().Next(),
            Elements = new List<AssetElement>
            {
                new(){ Id = 1},//ID's need to be the same as before
                new(){ Id = 2}
            }
        };

        Console.WriteLine($"Before update is: {destination.Name} {destination.Id} {destination.ItemNumber} {destination.ItemName}");
        destination.Elements.ForEach(e => Console.WriteLine($" {e.Id} {e.Text}"));

        mapper.Map(source_update, destination);

        Console.WriteLine($"After update is : {destination.Name} {destination.Id} {destination.ItemNumber} {destination.ItemName}");
        destination.Elements.ForEach(e => Console.WriteLine($" {e.Id} {e.Text}"));
    }
}

#region Source

public class Source
{
    public string Name;
    public int Id;

    public List<AssetElement> Elements;

}

public class AssetElement
{
    public int Id;
}


#endregion

#region Destination

public class Destination
{
    public string Name;
    public int Id;

    public List<AssetElementDto> Elements;

    public int ItemNumber;
    public string ItemName;
}

public class AssetElementDto
{
    public string Text;
    public int Id;
}

#endregion

安慰:

Wow更新后保留在收藏中。

Before update is: Sinonis 49772274 42 NPC21
 1 Wow
 2 Wow
After update is : Nindalf 236466397 42 NPC21
 1 Wow
 2 Wow

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    相关资源
    最近更新 更多