How to: Copy an instance of an object or entire graph using Open Access ORM

1 posts, 0 answers
  1. 63F75A2C-1F16-4AED-AFE8-B1BBD57646AD
    63F75A2C-1F16-4AED-AFE8-B1BBD57646AD avatar
    1572 posts
    Member since:
    Oct 2004

    Posted 11 Jul 2013 Link to this post


    OpenAccess ORM version
    .NET version .NET Framework 4.0
    Visual Studio version VS 2010
    programming language C#, VB
    browser support

    all browsers supported by RadControls

    This project shows how to copy an object or an entire graph in the database.

    To achieve this goal we should first detach an entity from the context using the CreateDetachedCopy method with the proper fetch strategy to detach the whole graph, serialize the graph, deserialize it and Add it to the context. 

    The project includes both the custom code generation templates which modifies the domain classes and add [Serializable] and a sample Domain Model defined using the template. The changes in the code generation templates could be founded in the OpenAccess GitHub repository - openaccess-templates / DomainModel / Serializable Entities / Version1 /.

    For further information how to customize code generation templates refer to this article:

    1) The first step is to detach the category from the database with the proper fetch strategy.

    2) The second step is to serialize and deserialize the detached entities. This step should be done to remove the persistence tracking functionality of OpenAccess from the entity so the objects will be marked as new.

    In the samples the Domain Model class is extended with partial class and there is added two methods CreateDeepCopy<T> and they could be used as extension methods from the dbContext.

    01.public T CreateDeepCopy<T>(T singleEntity)
    03.    return this.CreateDeepCopy<T>(singleEntity, null);
    06.public T CreateDeepCopy<T>(T entity, FetchStrategy fetchStrategy)
    08.    using (MemoryStream stream = new MemoryStream())
    09.    {
    10.        T detachedEntity = this.CreateDetachedCopy<T>(entity, fetchStrategy);
    12.        BinaryFormatter formatter = new BinaryFormatter();
    13.        formatter.Serialize(stream, detachedEntity);
    14.        stream.Seek(0, SeekOrigin.Begin);
    15.        T copy = (T)formatter.Deserialize(stream);
    17.        return copy;
    18.    }

    01.Public Function CreateDeepCopy(Of T)(ByVal singleEntity As T) As T
    02.    Return Me.CreateDeepCopy(Of T)(singleEntity, Nothing)
    03.End Function
    05.Public Function CreateDeepCopy(Of T)(ByVal entity As T, ByVal fetchStrat As FetchStrategy) As T
    06.    Using stream As New MemoryStream()
    07.        Dim detachedEntity As T = Me.CreateDetachedCopy(Of T)(entity, fetchStrat)
    09.        Dim formatter As New BinaryFormatter()
    10.        formatter.Serialize(stream, detachedEntity)
    11.        stream.Seek(0, SeekOrigin.Begin)
    12.        Dim copy As T = CType(formatter.Deserialize(stream), T)
    14.        Return copy
    15.    End Using
    16.End Function


    The Northwind database
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.