locked
Check if entity property changed then assign it RRS feed

  • Question

  • Hi everyone,

    I am writing the code to sync two databases. I update entities in second db. And I would like to know if I should check if the property is changed like this:

    if (newEntity.Prop != oldEntity.Prop)
    newEntity.Prop = oldEntity.Prop;

    Or I can assign all the new values to old entity and save the changes?

    Thank you for your replies.

    Sunday, December 30, 2012 6:49 PM

Answers

  • Hi Jiří,

    If all the entities are changed, in this scenario, the way assigning values directly have the performance advantage. If some of entities are not changed, it is hard to say which is better. For example, sometimes, only a few entities are changed. In this scenario, iterating all property of all entities may exhaust a lot of time, but the generated sql only affect a few entities, so the generated sql executes more faster on server. I think which approach is up to your problem and its background.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jiří Straka Sunday, January 6, 2013 12:01 AM
    Wednesday, January 2, 2013 7:18 AM

All replies

  • To sync two databases, please try Database http://msdn.microsoft.com/en-us/sync/bb887608.aspx 

    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Monday, December 31, 2012 6:57 AM
  • Hi Jiří,

    If all the entities are changed, in this scenario, the way assigning values directly have the performance advantage. If some of entities are not changed, it is hard to say which is better. For example, sometimes, only a few entities are changed. In this scenario, iterating all property of all entities may exhaust a lot of time, but the generated sql only affect a few entities, so the generated sql executes more faster on server. I think which approach is up to your problem and its background.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jiří Straka Sunday, January 6, 2013 12:01 AM
    Wednesday, January 2, 2013 7:18 AM
  • In my scenario I know that something has been changed in my entity but I dont know if it is one property or all its properties. So check before assignment is better in this case?
    Wednesday, January 2, 2013 5:12 PM
  • Hi Jiří,

    For this scenario, I think you are not required to check each property. Just create a new entity object and assign the latest values. Then EF will generate the sql to update the record in database which have the same primary key(s).

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 3, 2013 8:24 AM