none
How to clone an ORM row? RRS feed

  • Question

  • Hi,

     

    I'm using LINQ to retrieve data from a sql database, and to update the items I loaded I'd like to use the attach-call with

    Attach(TEntity entity, TEntity original);

     

    My problem is, how do I create the original? TEntity (as created by the visual designer) does not contain a clone method.

     

    This should be easy, not?

     

    regards,

    Sam

    Monday, June 16, 2008 12:37 PM

Answers

All replies

  • Would Table<T>.GetOriginalEntityState( ) work for you?

     

    See http://msdn.microsoft.com/en-us/library/bb548700.aspx.

     

    Thanks,

     

    --Samir

     

    Monday, June 16, 2008 6:32 PM
  • Probably not - you see, I use LINQ to retrieve an entity from the database, I send the entity (as a result) to the client using WCF, and the client needs to clone the set, so it can check if an item has been changed.

     

    So I need a deep clone of the entity, but since it is not attached to any datacontext anymore (due to serialization), I can't access the original state (otherwise this would be perfect, though).

     

    thanks!

    Sam

    Tuesday, June 17, 2008 8:06 AM
  • Attaching the original is not the only way, you can also consider the other overload Attach(entity, true) what lets you to attach your entity in a changed status. (true : isModified)

    Tuesday, June 17, 2008 3:53 PM
  • Sidar, this does only work if your table contains some timestamp.

     

    Unfortunately the tables I have to use do not contain timestamps, so I'm out of luck there.

     

    regards,

    Sam

    Friday, June 20, 2008 1:50 PM
  • hi,

     

    A really nice and helpful blogger named whizzo helped me with some terrific code to deep-clone any object:

    http://whizzodev.blogspot.com/2008/06/object-deep-cloning-using-il-in-c_20.html

     

    A very interesting concept to clone an object, generating a function on the fly, so cloning will be blazingly fast after the function is generated.

     

    regards,
    Sam

    Monday, June 23, 2008 9:25 AM