简体   繁体   English

如何使用 Automapper 在目标 object 中保留集合的未映射属性?

[英]How to preserve the not mapped properties of a collection in a destination object with Automapper?

Initial Situation: I have a source and a destination object and both have a collection of Elements .初始情况:我有一个源和一个目标 object 并且都有一个Elements集合。 With Automapper I map the source to the destination object. After that, I add information to properties which are only present on the destination object ( ItemName and ItemNumber ).使用 Automapper I map 从源到目标 object。之后,我将信息添加到仅存在于目标 object( ItemNameItemNumber )上的属性。 Additionally, I add information to the property Text of the objects in the collection AssetElementDto .此外,我将信息添加到集合AssetElementDto中对象的属性Text After that, I call mapper.Map(source_update, destination);之后,我调用mapper.Map(source_update, destination); to update the destination object.更新目的地 object。

Question: When I run the code the information of ItemName and ItemNumber is preserved after the update.问题:当我运行代码时,更新后保留了ItemNameItemNumber的信息。 What can I do that also the information of the AssetElementDto of Text is preserved?我该怎么做才能保留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

Console Output:控制台 Output:

Wow is not preserved after the update.更新后Wow没有保留。

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

Thank you Lucian Bargaoanu for this brief but powerful hint.感谢Lucian Bargaoanu提供的这个简短而有力的提示。 This solved my problem.这解决了我的问题。 Please see the necessary changes in the code below.请在下面的代码中查看必要的更改。 Here is the link to AutoMapper.Collection.这是 AutoMapper.Collection 的链接

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

Console:安慰:

Wow is preserved in the collection after the update.更新后Wow保留在合集中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从属性创建一个对象,并使用automapper添加到目标集合 - Create one object from properties and add to collection of destination with automapper Automapper-如何将两个复杂的集合映射到一个目标集合对象 - Automapper - how to map two complex collections to one destination collection object 源中具有只读属性的 AutoMapper 行为未映射到目标 - AutoMapper behaviour with readonly properties in source not being mapped to destination Automapper如果source为null,则设置目标对象属性 - Automapper If source is null, set destination object properties AutoMapper将属性设置为目标对象上的null - AutoMapper settings properties to null on destination object 在 AutoMapper 中,目标类型上的只读集合属性不会被自动忽略 - In AutoMapper, read only collection properties on the destination type are not automatically ignored 自动映射器查找未映射的属性 - Automapper Finding Not Mapped properties 当目标是具有一些填充属性的现有对象时,AutoMapper 将目标对象中的属性归零 - AutoMapper nulls out properties in destination object when the destination is an existing object with some populated properties 如何告诉Automapper检查所有源属性是否都具有目标属性 - How to tell Automapper to check if all source properties have destination properties Automapper 2 个源字段映射到 1 个目标字段 - Automapper 2 source fields mapped to 1 destination field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM