logo
down
shadow

How to ReverseMap a complex object in AutoMapper


How to ReverseMap a complex object in AutoMapper

By : AnVir
Date : November 21 2020, 07:31 AM
should help you out Yes, you need to create the reverse mapping in this case. The reason for this is because AutoMapper wouldn't know how to instantiate the target Address property.
code :


Share : facebook icon twitter icon
Automapper: bidirectional mapping with ReverseMap() and ForMember()

Automapper: bidirectional mapping with ReverseMap() and ForMember()


By : BHOS
Date : March 29 2020, 07:55 AM
hop of those help? I have the case where I want to map an entity to a viewmodel and back. I have to specify the mapping explicitly with ForMember() because their properties do not share the exact same names. Here is a short example of how my classes look like: , You could define your configuration like this:
code :
Mapper.CreateMap<PartTwo, PartTwoViewModel>()
    .ForMember(dst => dst.PartInteger, opt => opt.MapFrom(src => src.Integer));

Mapper.CreateMap<PartTwoViewModel, PartTwo>()
    .ForMember(dst => dst.Integer, opt => opt.MapFrom(src => src.PartInteger));
Mapper.CreateMap<PartTwoViewModel, PartTwo>();
ReverseMap in Automapper using alternate naming conventions

ReverseMap in Automapper using alternate naming conventions


By : user3383704
Date : March 29 2020, 07:55 AM
this one helps. Here is my mapping config: , This is a bug, can you open a GitHub issue?
Automapper and complex object

Automapper and complex object


By : s.elizabeth
Date : March 29 2020, 07:55 AM
this will help @Lucian is correct. Collections are handled by default. So if your original mapping is setup correctly object => list then list => list> would work as well.
code :
           expression.CreateMap<ObjASource, List<MyDto>>()
                .ConvertUsing(source =>
                    {
                        return source.ObjB?.ObjC?.Select(p => new MyDto
                        {
                            field1 = source.field1,
                            field2 = source.field2,
                            field3 = source.field3,
                            field4 = p.fieldX,
                            field5 = p.fieldY
                        })
                        .ToList();
                    }
            );
        var test = new List<ObjASource>
        {
            new ObjASource
            {
                field1 = "1",
                field2 = "2",
                field3 = "3",
                ObjB = new ObjB
                {
                    ObjC = new List<ObjC>
                    {
                        new ObjC
                        {
                            fieldX = "X",
                            fieldY = "Y"
                        }
                    }
                }
            }
        };

        var result = Mapper.Map<List<List<MyDto>>>(test);
        var mapper = new MapperConfiguration(exp =>
        {
            exp.CreateMap<List<Product>, List<ProductDto>>()
                .ConstructUsing(products => products.SelectMany(product => product.company,
                    (product, companiesProvideProduct) => new ProductDto
                    {
                        product_id = product.product_id,
                        name = product.name,
                        company_id = companiesProvideProduct.company_id,
                        price = companiesProvideProduct.price
                    }).ToList());

            exp.CreateMap<Product, List<ProductDto>>()
                .ConvertUsing(product => product.company.Select(provideProduct => new ProductDto
                {
                    product_id = product.product_id,
                    name = product.name,
                    company_id = provideProduct.company_id,
                    price = provideProduct.price
                }).ToList());
        }).CreateMapper();
        var productList = new List<Product>
        {
            new Product
            {
                product_id = 1,
                name = "test1",
                company = new List<CompaniesProvideProduct>
                {
                    new CompaniesProvideProduct
                    {
                        company_id = 1,
                        price = 1.99m,
                        product_id = 1
                    }
                }
            },
            new Product
            {
                product_id = 2,
                name = "test2",
                company = new List<CompaniesProvideProduct>
                {
                    new CompaniesProvideProduct
                    {
                        company_id = 2,
                        price = 1.99m,
                        product_id = 2
                    }
                }
            }
        };
var result1 = mapper.Map<List<ProductDto>>(productList);
var result2 = mapper.Map<List<ProductDto>>(productList.First());
Can't configure ReverseMap in AutoMapper

Can't configure ReverseMap in AutoMapper


By : Isabela Martins
Date : March 29 2020, 07:55 AM
it helps some times I'm a AutoMapper newb. My mappings are not working as expected and I'm sure I'm doing somthing wrong but can't figure it out. Sorry if this question is confusing, but i'll do my best to be clear. Lets say we have three classes: , Here's one way of doing it:
code :
var autoMapperConfig = new AutoMapper.MapperConfiguration(cfg =>
{
    cfg.CreateMap<ContactInfo1, ContactInfo2>()
        .ForMember(dest => dest.AnotherName, opt => opt.MapFrom(src => src.Name))
        .ReverseMap();
    cfg.CreateMap<Person, ContactInfo2>()
        .ConstructUsing((p, ctx) => ctx.Mapper.Map<ContactInfo2>(p.Contact));
});
Automapper - ReverseMap() does not perform mapping

Automapper - ReverseMap() does not perform mapping


By : gunther marktmeinung
Date : March 29 2020, 07:55 AM
hope this fix your issue ReverseMap only creates a simple reverse mapping. For example it would automatically configure
Related Posts Related Posts :
  • Stop Continuation Task upon exception thrown from parent task
  • Is there a way to make msbuild write error output to stderr?
  • Unit testing: TDD with POCO Objects with navigation properties (relationship fixup)
  • GridSplitter disables my RowDefinition style
  • Memory leak only in Release mode
  • how to read specified string from url
  • how to store html code EMail template in a string variable or textbox c#
  • Set Selected Date to TimePicker wpf (TimePickers inside DataTemplate)
  • How to draw line in Silverlight Specifying Height?
  • Stored procedure executes but does not update data
  • ASP.NET ListBox Trouble
  • Why does C# also not allow empty conditions in while loops?
  • Unable to get a block of code into my regex match groups
  • What is difference between dbcontext.Add and dbcontext.AddObject
  • How do I update the file version number of C# DLL without recompiling?
  • Value for html control always null
  • HttpClient hangs when timeout is setting (Windows Phone)
  • Handle Multiple Form tag in asp.net page?
  • create word document with html content in c#
  • Confusion about Find And Replace
  • Format sms messages in Clickatell
  • Automated Function Overload
  • String to date in MS Access SQL statement gives type mismatch error
  • ShowDialog exiting on certain events
  • Is there a try Convert.ToInt32... avoiding exceptions
  • How to move wpf application into minimize tray at Window Start-up C#?
  • How to change display format of long variable?
  • How to use Addfields in MongoDB C# Aggregation Pipeline
  • MsTest TestCleanup method not called when an unhandled exception is thrown
  • missing last data when exporting gridview to excel
  • How to add array of objects to List in c#
  • Lambda Expression to order (sort) my list collection
  • Library for displaying music notation
  • How to compare two dictionaries in c# and get the output as True and False after validation
  • specify fields to be serialized with JSON
  • How do you obtain the content of a specific node using XmlDocument in C#?
  • How to ignore the first line in a csv file when you read the csv file in C#
  • c# - Problem calling public void from class
  • How to tell a class which objects it should create? Type vs. object confusion :(
  • Save CheckBox state to xml
  • WIX CAQuietExec NETSH Command Fails
  • Issues sending http put request every 60 seconds to RoR app
  • ConfigurationManager.ConnectionStrings.ConnectionString Issue
  • Real size WPF controls for printing
  • How to cancel properly?
  • C# String multiplication error
  • Using Solrnet and Assigning Attributes with Entity Framework Generated POCOs
  • Regex pattern for single backslash
  • TextBox: insert spaces for credit card number?
  • C# DLL loaded for exe-application is not found when launching similar DLL by rundll32.exe
  • Debug a Windows Service with WCF library
  • Open a file from an external assembly?
  • Servicestack RegistrationFeature Unable to bind request
  • Index was outside the bounds of the array confusion
  • Error in Xml to List code. The ':' character, hexadecimal value 0x3A, cannot be included in a name
  • I am trying to do a while loop with a string conditional statement in C#
  • C# 'Cannot access a disposed object. Object name: 'SslStream'.'
  • How to make Gecko use seperate CookieContainer per instance?
  • C# Advanced form "please wait"
  • Send and Receive data C# using network stream
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org