none
How can I convert an entity object to another one with same properties from a different model RRS feed

  • Question

  • I have two completely same sql server express databases in my webform application. One of them is primary and the other one is for temporary uses. So I've added two entity model to my app for each of them. now I want to compare two records of these two models with together and save the temporary one in primary database if needed. In order to do this I need a converter that act like this:

    PrimaryModel.Table primaryObjet = ...; // finding the related object from the primary database
    TemporaryModel.Table tmpObject = ...;
    
    if(primaryObjet != ConvertToPrimaryObj(tmpObject))
    {
    db.table.ApplyCurrentValues(ConvertToPrimaryObj(tmpObject)); // adding converted temporary object to the primary database
    db.SaveChanges();
    }

    My database has one primary table which has too many properties and is connected to some tables.

    At first I decided to wrote my own converter but however I did that it does not work properly and when two objects are completely the same it says they are not. I searched and found AutoMapper.dll but that was useless too maybe because I am not familiar with that dll enough.

    Can anyone help me?


    • Edited by Monoloox Tuesday, July 31, 2012 11:03 AM
    Tuesday, July 31, 2012 11:00 AM

Answers

  • Hi Monoloox,

    Welcome to the MSDN forum.

    I think you can try something like this:

                    var dbset = primaryContext.Tables;
                    foreach (var p in dbset)
                    {
                        var another = tempContext.Unicorns.Find(p.Id);//If you have two contexts, you can use a DTO class to cache the data.
                        foreach (var u in primaryContext.Entry(p).CurrentValues.PropertyNames)
                        {
                            if (tempContext.Entry(another).Property(u).CurrentValue == primaryContext.Entry(p).Property(u).CurrentValue)
                            {
                                Console.WriteLine("Property name: {0}, Equals", u);
                            }
                            else
                            {
                                //Update the temp values here.
                            }
                        }
                    }

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, August 1, 2012 5:28 AM

All replies

  • Hi Monoloox,

    Welcome to the MSDN forum.

    I think you can try something like this:

                    var dbset = primaryContext.Tables;
                    foreach (var p in dbset)
                    {
                        var another = tempContext.Unicorns.Find(p.Id);//If you have two contexts, you can use a DTO class to cache the data.
                        foreach (var u in primaryContext.Entry(p).CurrentValues.PropertyNames)
                        {
                            if (tempContext.Entry(another).Property(u).CurrentValue == primaryContext.Entry(p).Property(u).CurrentValue)
                            {
                                Console.WriteLine("Property name: {0}, Equals", u);
                            }
                            else
                            {
                                //Update the temp values here.
                            }
                        }
                    }

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, August 1, 2012 5:28 AM
  • Thanks for your reply

    I used your idea and made some changes on your code and that worked for me. Thank you


    • Edited by Monoloox Saturday, August 4, 2012 6:26 AM
    Saturday, August 4, 2012 6:25 AM