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

    Requirements

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

    all browsers supported by RadControls


    PROJECT DESCRIPTION
    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:
    http://documentation.telerik.com/openaccess-orm/developers-guide/code-generation/customizing-code-generation/openaccess-tasks-customise-code-generation-overview

    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.

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

    VB
    01.Public Function CreateDeepCopy(Of T)(ByVal singleEntity As T) As T
    02.    Return Me.CreateDeepCopy(Of T)(singleEntity, Nothing)
    03.End Function
    04. 
    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)
    08. 
    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)
    13. 
    14.        Return copy
    15.    End Using
    16.End Function

    Prerequisites

    The Northwind database
Back to Top

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