Using Automapper when Updating an object and all of its children

Thread is closed for posting
4 posts, 0 answers
  1. Ryan Lege
    Ryan  Lege avatar
    14 posts
    Member since:
    Jul 2009

    Posted 01 Aug 2011 Link to this post

    I have an interesting situation where I have to replicate data from one DB to another and the objects in structure are identical. But I'm trying to use automapper to map the source object to it's counterpart instance to update it to current data. Unfortunately OpenAccess does not seem to see the changes up and down the entire tree when I invoke to SaveChanges() method. Has anyone been able to get this to work for them?
  2. Nikola
    Nikola avatar
    27 posts

    Posted 02 Aug 2011 Link to this post

    Hi Ryan Lege,

    Please find attached demo project and SQL script.
    The SQL script contains the schema of a *source* Database including some demo data. The solution also uses a *destination* database which is exactly the same as the source one, with the exception of having any data in it. The solution takes all the records from the source and inserts it into the destination. Objects are transferred using AutoMapper.
    Please bear in mind that you will have to update the reference of AutoMapper.dll.

    If the above example does not help you solve the problem, please send us a sample project (your automapper configuration will be necessary), so we can look into it.

    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  3. Nikesh
    Nikesh avatar
    12 posts
    Member since:
    Nov 2011

    Posted 03 Apr 2012 Link to this post

    THIS DOES NOT WORK ON UPDATES. Can someone help.

    Looked at the solution at it only seems to work for inserts NOT updates. I changed the code to do updates as follows but .GetUpdates() returns 0:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AutoMapper;
    using SourceLibrary;
     namespace Destination
        class Program
             private static void InitializeMapper()
                Mapper.CreateMap<SourceLibrary.ORDER, ORDER>();
            static void Main(string[] args)
                using (DestinationDBContext destinationDBContext = new DestinationDBContext())
                    ////clear the destination tables
                    //foreach( ORDER destinationOrder in destinationDBContext.ORDERs )
                    //    destinationDBContext.Delete( destinationOrder );
                    Console.WriteLine( "Destination orders count == " + destinationDBContext.ORDERs.Count() );
                    using (SourceDBContext dbContext = new SourceDBContext())
                        IList<SourceLibrary.ORDER> orders = dbContext.ORDERs.ToList();
                        foreach (SourceLibrary.ORDER sourceOrder in orders)
                            ORDER orderDestination = destinationDBContext.ORDERs.FirstOrDefault(r => r.ORDER_ID == sourceOrder
                            orderDestination = Mapper.Map<SourceLibrary.ORDER, ORDER>(sourceOrder);
                    Console.WriteLine( "Updated orders = " + destinationDBContext.GetChanges().GetUpdates<ORDER>().Count );
                   // Console.WriteLine( "Destination orders count after insert == " + destinationDBContext.ORDERs.Count() );

  4. PetarP
    PetarP avatar
    754 posts

    Posted 06 Apr 2012 Link to this post

    Hi Nikesh,

     In order for the GetUpdates api to work correctly you will need to make changes to an object that is already tracked by our context. In your case the changes you are making are done on objects that are not being tracked by the context and thus you are not getting any result from the method.

    Kind regards,
    the Telerik team
    Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
Back to Top