This is a migrated thread and some comments may be shown as answers.
Using Automapper when Updating an object and all of its children
3 Answers 46 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ryan Lege
Top achievements
Rank 1
Ryan Lege asked on 01 Aug 2011, 08:43 AM
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?

3 Answers, 1 is accepted

Sort by
0
Nikola
Telerik team
answered on 02 Aug 2011, 04:52 PM
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.

Regards,
Nikola
the Telerik team

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

0
Nikesh
Top achievements
Rank 1
answered on 03 Apr 2012, 07:20 PM
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)
        {
            InitializeMapper();
 
            using (DestinationDBContext destinationDBContext = new DestinationDBContext())
            {
                ////clear the destination tables
                //foreach( ORDER destinationOrder in destinationDBContext.ORDERs )
                //{
                //    destinationDBContext.Delete( destinationOrder );
                //}
                //destinationDBContext.SaveChanges();
                 
                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
.ORDER_ID);
                        orderDestination = Mapper.Map<SourceLibrary.ORDER, ORDER>(sourceOrder);
                        destinationDBContext.SaveChanges();
                        //Add(orderDestination);
                    }
                }
                Console.WriteLine( "Updated orders = " + destinationDBContext.GetChanges().GetUpdates<ORDER>().Count );
                destinationDBContext.SaveChanges();
               // Console.WriteLine( "Destination orders count after insert == " + destinationDBContext.ORDERs.Count() );
            }
 
            Console.ReadLine();
        }
    }
}


0
PetarP
Telerik team
answered on 06 Apr 2012, 01:34 PM
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,
Petar
the Telerik team
Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
Tags
General Discussions
Asked by
Ryan Lege
Top achievements
Rank 1
Answers by
Nikola
Telerik team
Nikesh
Top achievements
Rank 1
PetarP
Telerik team
Share this question
or