locked
Is There Any Easy Way To Duplicate An Entity ? RRS feed

  • Question

  • Does anybody know if there is any easy way to duplicate an entity ?

    I have an entity with 40-50 properties, and a number of child collections. I want to be able to copy it, and have the DB treat this as a new row when the object is saved ?

    The DB will automatically allocate it a unique ID as the key field is auto incrementing.

    Is there an easy way of doing this without iterating through all the properties and child collections ?

    Thanks,

    Nick

    Friday, July 15, 2011 10:14 AM

Answers

  • VB:
        Public NotInheritable Class EntityCloner
            Private Sub New()
            End Sub
     
            ' Perform a deep Copy of the object.
            Public Shared Function Clone(Of T)(ByVal source As T) As T
                If (Not GetType(T).IsSerializable) _
                Then
                    Throw New ArgumentException("The type must be serializable.""source")
                End If
     
                ' Don't serialize a null object, simply return the default for that object
                If Object.ReferenceEquals(source, Nothing) _
                Then
                    Return Nothing
                End If
     
                Dim formatter As IFormatter = New BinaryFormatter()
                Dim stream As Stream = New MemoryStream()
     
                Using stream
                    formatter.Serialize(stream, source)
                    stream.Seek(0, SeekOrigin.Begin)
     
                    Return CType(formatter.Deserialize(stream), T)
                End Using
            End Function     
    End Class
    C#:
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    namespace EF
    {
    // http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
    public sealed class EntityCloner
    {
    private EntityCloner()
    {
    }
    // Perform a deep Copy of the object.
    public static T Clone<T>(T source)
    {
    if (! (typeof(T).IsSerializable))
    {
    throw new ArgumentException("The type must be serializable.", "source");
    }
    // Don't serialize a null object, simply return the default for that object
    if (object.ReferenceEquals(source, null))
    {
    return default(T);
    }
    IFormatter formatter = new BinaryFormatter();
    Stream stream = new MemoryStream();
    using (stream)
    {
    formatter.Serialize(stream, source);
    stream.Seek(0, SeekOrigin.Begin);
    return (T)(formatter.Deserialize(stream));
    }
    }
    }
    }

    Yann

    (plus ça change, plus c'est la même chose!)

    Friday, July 15, 2011 11:58 AM

All replies

  • VB:
        Public NotInheritable Class EntityCloner
            Private Sub New()
            End Sub
     
            ' Perform a deep Copy of the object.
            Public Shared Function Clone(Of T)(ByVal source As T) As T
                If (Not GetType(T).IsSerializable) _
                Then
                    Throw New ArgumentException("The type must be serializable.""source")
                End If
     
                ' Don't serialize a null object, simply return the default for that object
                If Object.ReferenceEquals(source, Nothing) _
                Then
                    Return Nothing
                End If
     
                Dim formatter As IFormatter = New BinaryFormatter()
                Dim stream As Stream = New MemoryStream()
     
                Using stream
                    formatter.Serialize(stream, source)
                    stream.Seek(0, SeekOrigin.Begin)
     
                    Return CType(formatter.Deserialize(stream), T)
                End Using
            End Function     
    End Class
    C#:
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    namespace EF
    {
    // http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
    public sealed class EntityCloner
    {
    private EntityCloner()
    {
    }
    // Perform a deep Copy of the object.
    public static T Clone<T>(T source)
    {
    if (! (typeof(T).IsSerializable))
    {
    throw new ArgumentException("The type must be serializable.", "source");
    }
    // Don't serialize a null object, simply return the default for that object
    if (object.ReferenceEquals(source, null))
    {
    return default(T);
    }
    IFormatter formatter = new BinaryFormatter();
    Stream stream = new MemoryStream();
    using (stream)
    {
    formatter.Serialize(stream, source);
    stream.Seek(0, SeekOrigin.Begin);
    return (T)(formatter.Deserialize(stream));
    }
    }
    }
    }

    Yann

    (plus ça change, plus c'est la même chose!)

    Friday, July 15, 2011 11:58 AM
  • Jaime,

    Nick asked "Is there an easy way of doing this without iterating through all the properties and child collections". Beth's video uses exactly that method to add a duplicate row in a collection, property by property, which is exactly what Nick did NOT want.


    Yann

    (plus ça change, plus c'est la même chose!)

    Friday, July 15, 2011 2:46 PM
  • Yann is always right Jaime, lol!

    Thank you both for your input - I could not have figured that one out on my own!

    Nick

     

    Saturday, July 16, 2011 5:31 AM
  • Oh I SO much wanted to make that joke myself, but I didn't really think I should, hehehehehe..

    Yann

    (plus ça change, plus c'est la même chose!)

    Saturday, July 16, 2011 5:42 AM
  • Modest too Yann! :o)

    Saturday, July 16, 2011 6:26 AM
  • Nice piece of code thanks for this Yann
    David Severn
    Saturday, July 16, 2011 12:20 PM
  • You're welcome.

    Yann

    (plus ça change, plus c'est la même chose!)

    Saturday, July 16, 2011 2:43 PM